Skip to content

Commit

Permalink
Replace memory addresses with UUIDs to enhance the uniqueness of temp…
Browse files Browse the repository at this point in the history
…late_name (#210)

* replace memory addresses with UUIDs to enhance the uniqueness of template names

* fix tests
  • Loading branch information
ilkilic authored Aug 20, 2024
1 parent 5406434 commit d4b3597
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
5 changes: 3 additions & 2 deletions bluecellulab/cell/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import re
import string
from typing import NamedTuple, Optional
import uuid

import neuron

Expand Down Expand Up @@ -126,8 +127,8 @@ def load(self, template_filename: str) -> str:
# templates load outside of bluecellulab
template_name = "%s_bluecellulab" % template_name
template_name = get_neuron_compliant_template_name(template_name)
obj_address = hex(id(self))
template_name = f"{template_name}_{obj_address}"
unique_id = uuid.uuid4().hex
template_name = f"{template_name}_{unique_id}"

template_content = re.sub(
r"begintemplate\s*(\S*)",
Expand Down
10 changes: 8 additions & 2 deletions tests/test_cell/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import random
import warnings
from pathlib import Path
from unittest.mock import patch
import uuid

import neuron
import numpy as np
Expand Down Expand Up @@ -47,13 +49,17 @@ def test_longname():


@pytest.mark.v5
def test_load_template():
@patch('uuid.uuid4')
def test_load_template(mock_uuid):
"""Test the neuron template loading."""
id = '12345678123456781234567812345678'
mock_uuid.return_value = uuid.UUID(id)

hoc_path = parent_dir / "examples/cell_example1/test_cell.hoc"
morph_path = parent_dir / "examples/cell_example1/test_cell.asc"
template = NeuronTemplate(hoc_path, morph_path, "v5", None)
template_name = template.template_name
assert template_name == f"test_cell_bluecellulab_{hex(id(template))}"
assert template_name == f"test_cell_bluecellulab_{id}"


def test_shorten_and_hash_string():
Expand Down
12 changes: 9 additions & 3 deletions tests/test_cell/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"""Unit tests for template.py module."""

from pathlib import Path
from unittest.mock import Mock
from unittest.mock import Mock, patch
import uuid

import pytest

Expand Down Expand Up @@ -44,11 +45,16 @@
)


def test_get_cell_with_bluepyopt_template():
@patch('uuid.uuid4')
def test_get_cell_with_bluepyopt_template(mock_uuid):
"""Unit test for the get_cell method with bluepyopt_template."""

id = '12345678123456781234567812345678'
mock_uuid.return_value = uuid.UUID(id)
template = NeuronTemplate(hipp_hoc_path, hipp_morph_path, "bluepyopt", None)
cell = template.get_cell(gid=None)
assert cell.hname() == f"bACnoljp_bluecellulab_{(hex(id(template)))}[0]"

assert cell.hname() == f"bACnoljp_bluecellulab_{id}[0]"


def test_neuron_template_init():
Expand Down

0 comments on commit d4b3597

Please sign in to comment.