Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle run_args in device.delete #1573

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions brian2/devices/cpp_standalone/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ def __init__(self):
#: Dict of all static saved arrays
self.static_arrays = {}

#: Names of static arrays used for run_args given as lists of values
self.run_args_arrays = []

#: Dict of all TimedArray objects
self.timed_arrays = {}

Expand Down Expand Up @@ -1243,6 +1246,7 @@ def run(
with FileLock(fname + ".lock"):
if not os.path.exists(fname):
value_ar.tofile(fname)
self.run_args_arrays.append(value_name)
list_rep.append(f"{name}={string_value}")

run_args = list_rep
Expand Down Expand Up @@ -1632,11 +1636,11 @@ def build(
]
logger.debug(f"Time measurements: {', '.join(logged_times)}")

def delete(self, code=True, data=True, directory=True, force=False):
def delete(self, code=True, data=True, run_args=True, directory=True, force=False):
if self.project_dir is None:
return # Nothing to delete

if directory and not (code and data):
if directory and not all([code, data, run_args]):
raise ValueError(
"When deleting the directory, code and data will"
"be deleted as well. Set the corresponding "
Expand Down Expand Up @@ -1691,6 +1695,10 @@ def delete(self, code=True, data=True, directory=True, force=False):
for static_array_name in self.static_arrays:
fnames.append(os.path.join("static_arrays", static_array_name))

if run_args:
for fname in self.run_args_arrays:
fnames.append(os.path.join("static_arrays", fname))

for fname in fnames:
full_fname = os.path.join(self.project_dir, fname)
try:
Expand Down
21 changes: 16 additions & 5 deletions brian2/tests/test_cpp_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,23 +567,34 @@ def test_profile_via_set_device_arg():
@pytest.mark.cpp_standalone
@pytest.mark.standalone_only
def test_delete_code_data():
set_device("cpp_standalone", build_on_run=True, directory=None)
set_device("cpp_standalone", build_on_run=False)
group = NeuronGroup(10, "dv/dt = -v / (10*ms) : volt", method="exact")
group.v = np.arange(10) * mV # uses the static array mechanism
run(defaultclock.dt)
# Overwrite the initial values via run_args mechanims
device.build(run_args={group.v: np.arange(10)[::-1] * mV}, directory=None)
results_dir = os.path.join(device.project_dir, "results")
assert os.path.exists(results_dir) and os.path.isdir(results_dir)
# There should be 3 files for the clock, 2 for the neurongroup (index + v),
# and the "last_run_info.txt" file
assert len(os.listdir(results_dir)) == 6
device.delete(data=True, code=False, directory=False)
device.delete(data=True, run_args=False, code=False, directory=False)
assert os.path.exists(results_dir) and os.path.isdir(results_dir)
assert len(os.listdir(results_dir)) == 0
assert len(os.listdir(os.path.join(device.project_dir, "static_arrays"))) > 0
static_arrays_before = len(
os.listdir(os.path.join(device.project_dir, "static_arrays"))
)
assert static_arrays_before > 0
assert len(os.listdir(os.path.join(device.project_dir, "code_objects"))) > 0
device.delete(data=False, code=True, directory=False)
assert len(os.listdir(os.path.join(device.project_dir, "static_arrays"))) == 0
device.delete(data=False, code=True, run_args=False, directory=False)
assert (
0
< len(os.listdir(os.path.join(device.project_dir, "static_arrays")))
< static_arrays_before
)
assert len(os.listdir(os.path.join(device.project_dir, "code_objects"))) == 0
device.delete(data=False, code=False, run_args=True, directory=False)
len(os.listdir(os.path.join(device.project_dir, "static_arrays"))) == 0


@pytest.mark.cpp_standalone
Expand Down
Loading