Skip to content

Commit

Permalink
Use tmp_path fixture instead of creating new tmp files
Browse files Browse the repository at this point in the history
  • Loading branch information
jorblancoa committed Oct 9, 2024
1 parent dea058f commit f85b27a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 48 deletions.
18 changes: 16 additions & 2 deletions tests/integration-e2e/test_crashmode.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@


def test_crash_test_loading(SIM_DIR, tmp_path):
import json
from neurodamus import Node
from neurodamus.cell_distributor import CellDistributor
from neurodamus.metype import PointCell
from neuron import nrn
sim_dir = SIM_DIR / "v5_sonata"
sim_config_path = sim_dir / "simulation_config_mini.json"
config_file_mini = "simulation_config_mini.json"

n = Node(str(sim_config_path), {"crash_test": True})
# Read the original config file
sim_config_path = sim_dir / config_file_mini
with open(sim_config_path, "r") as f:
sim_config_data = json.load(f)

# Update the network path in the config
sim_config_data["network"] = str(sim_dir / "sub_mini5" / "circuit_config.json")

# Write the modified config to the temporary directory
temp_config_path = tmp_path / config_file_mini
with open(temp_config_path, "w") as f:
json.dump(sim_config_data, f, indent=2)

n = Node(str(temp_config_path), {"crash_test": True})
n.load_targets()
n.create_cells()

Expand Down
24 changes: 19 additions & 5 deletions tests/integration-e2e/test_multicycle_runs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import json
import os
import pytest
from pathlib import Path
Expand Down Expand Up @@ -69,15 +70,28 @@ def _read_sonata_spike_file(spike_file):
return timestamps, spike_gids


def test_v5_sonata_multisteps(capsys):
def test_v5_sonata_multisteps(capsys, tmp_path):
import numpy.testing as npt
from neurodamus import Neurodamus

config_file = str(SIM_DIR / "v5_sonata" / "simulation_config_mini.json")
output_dir = str(SIM_DIR / "v5_sonata" / "output_coreneuron")
tmp_file = _create_tmpconfig_coreneuron(config_file)
config_file = SIM_DIR / "v5_sonata" / "simulation_config_mini.json"
with open(config_file, "r") as f:
config_data = json.load(f)

# Modify the config for CORENEURON
config_data["target_simulator"] = "CORENEURON"
# Update the network path in the config
config_data["network"] = str(SIM_DIR / "v5_sonata" / "sub_mini5" / "circuit_config.json")

# Use temporary directory for output
output_dir = str(tmp_path / config_data["output"]["output_dir"])

# Write the modified config to the temporary directory
temp_config_path = tmp_path / "simulation_config_mini.json"
with open(temp_config_path, "w") as f:
json.dump(config_data, f, indent=2)

nd = Neurodamus(tmp_file.name, output_path=output_dir, modelbuilding_steps=3)
nd = Neurodamus(str(temp_config_path), output_path=output_dir, modelbuilding_steps=3)
nd.run()

# compare spikes with refs
Expand Down
36 changes: 19 additions & 17 deletions tests/integration-e2e/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ def _read_sonata_report(report_file):
return node_ids, data


def _create_tmpconfig_lfp(config_file):

import json
from tempfile import NamedTemporaryFile
with open(config_file, 'r') as f:
def _create_reports_config(original_config_path: Path, tmp_path: Path) -> tuple[Path, Path]:
"""
Create a modified configuration file in a temporary directory.
"""
# Read the original config file
with open(original_config_path, 'r') as f:
config = json.load(f)

# Update the network path in the config
config["network"] = str(SIM_DIR / "sub_mini5" / "circuit_config.json")

# Modify the necessary fields
config["reports"] = config.get("reports", {})
config["reports"]["summation_report"] = {
Expand All @@ -108,27 +112,25 @@ def _create_tmpconfig_lfp(config_file):
"end_time": 40.0
}

# Get the directory of the original config file
config_dir = Path(config_file).parent

# Write the modified configuration to a temporary file
tmp_file = NamedTemporaryFile(suffix=".json", delete=False, dir=config_dir, mode='w')
json.dump(config, tmp_file, indent=4)
tmp_file.close()
temp_config_path = tmp_path / "reports_config.json"
with open(temp_config_path, "w") as f:
json.dump(config, f, indent=4)

return tmp_file
# Create output directory
output_dir = tmp_path / config["output"]["output_dir"]

return str(temp_config_path), str(output_dir)

@pytest.mark.slow
def test_v5_sonata_reports():
def test_v5_sonata_reports(tmp_path):
import numpy.testing as npt
from neurodamus import Neurodamus

config_file = str(SIM_DIR / "simulation_config_mini.json")
output_dir = str(SIM_DIR / "output_reports")
config_file = SIM_DIR / "simulation_config_mini.json"
temp_config_path, output_dir = _create_reports_config(config_file, tmp_path)

tmp_file = _create_tmpconfig_lfp(config_file)
nd = Neurodamus(tmp_file.name, output_path=output_dir)
nd = Neurodamus(temp_config_path, output_path=output_dir)
nd.run()

report_refs = {
Expand Down
51 changes: 27 additions & 24 deletions tests/scientific/test_lfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,27 @@ def test_number_electrodes(test_file):
assert result == expected_result, f'Expected {expected_result}, but got {result}'


def _create_tmpconfig_lfp(config_file, lfp_file):
def _read_sonata_lfp_file(lfp_file):
import libsonata
report = libsonata.ElementReportReader(lfp_file)
pop_name = report.get_population_names()[0]
node_ids = report[pop_name].get_node_ids()
data = report[pop_name].get()
return node_ids, data


def _create_lfp_config(original_config_path: Path, lfp_file: Path, tmp_path: Path) -> tuple[Path, Path]:
"""
Create a modified lfp configuration file in a temporary directory.
"""
import json
from tempfile import NamedTemporaryFile
with open(config_file, 'r') as f:
# Read the original config file
with open(original_config_path, 'r') as f:
config = json.load(f)

# Update the network path in the config
config["network"] = str(SIM_DIR / "v5_sonata" / "sub_mini5" / "circuit_config.json")

# Modify the necessary fields
config["target_simulator"] = "CORENEURON"
config["run"]["electrodes_file"] = str(lfp_file)
Expand All @@ -151,37 +165,26 @@ def _create_tmpconfig_lfp(config_file, lfp_file):
"end_time": 1.0
}

# Get the directory of the original config file
config_dir = Path(config_file).parent

# Write the modified configuration to a temporary file
tmp_file = NamedTemporaryFile(suffix=".json", delete=False, dir=config_dir, mode='w')
json.dump(config, tmp_file, indent=4)
tmp_file.close()

return tmp_file
temp_config_path = tmp_path / "lfp_config.json"
with open(temp_config_path, "w") as f:
json.dump(config, f, indent=4)

# Create output directory
output_dir = tmp_path / config["output"]["output_dir"]

def _read_sonata_lfp_file(lfp_file):
import libsonata
report = libsonata.ElementReportReader(lfp_file)
pop_name = report.get_population_names()[0]
node_ids = report[pop_name].get_node_ids()
data = report[pop_name].get()
return node_ids, data
return str(temp_config_path), str(output_dir)


def test_v5_sonata_lfp(tmpdir, test_file):
def test_v5_sonata_lfp(tmpdir, test_file, tmp_path):
import numpy.testing as npt
from neurodamus import Neurodamus

config_file = str(SIM_DIR / "v5_sonata" / "simulation_config_mini.json")
output_dir = str(SIM_DIR / "v5_sonata" / "output_lfp")

config_file = SIM_DIR / "v5_sonata" / "simulation_config_mini.json"
lfp_weights_file = tmpdir.join("test_file.h5")
tmp_file = _create_tmpconfig_lfp(config_file, lfp_weights_file)
temp_config_path, output_dir = _create_lfp_config(config_file, lfp_weights_file, tmp_path)

nd = Neurodamus(tmp_file.name, output_path=output_dir)
nd = Neurodamus(temp_config_path, output_path=output_dir)
nd.run()

# compare results with refs
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit f85b27a

Please sign in to comment.