|
1 | 1 | import json |
2 | | -import os |
3 | 2 | import subprocess |
4 | | -import tempfile |
5 | 3 | from pathlib import Path |
6 | 4 |
|
| 5 | +import libsonata |
| 6 | + |
7 | 7 | SIM_DIR = Path(__file__).parent.parent.absolute() / "simulations" / "v5_sonata" |
8 | 8 | CONFIG_FILE_MINI = "simulation_config_mini.json" |
9 | 9 | CIRCUIT_DIR = "sub_mini5" |
10 | 10 |
|
11 | 11 |
|
12 | | -def test_cli_prcellgid(): |
13 | | - from neurodamus import Neurodamus |
14 | | - test_folder = tempfile.TemporaryDirectory("cli-test-prcellgid") # auto removed |
15 | | - test_folder_path = Path(test_folder.name) |
16 | | - with open(SIM_DIR / CONFIG_FILE_MINI, "r") as f: |
17 | | - sim_config_data = json.load(f) |
18 | | - sim_config_data["network"] = str(SIM_DIR / CIRCUIT_DIR / "circuit_config.json") |
19 | | - with open(test_folder_path / CONFIG_FILE_MINI, "w") as f: |
20 | | - json.dump(sim_config_data, f, indent=2) |
21 | | - |
22 | | - os.chdir(test_folder_path) |
23 | | - nd = Neurodamus(CONFIG_FILE_MINI, dump_cell_state=1, keep_build=True |
24 | | - ) |
25 | | - nd.run() |
26 | | - assert (test_folder_path / "output_sonata2" / "2_py_Neuron_t0.0.nrndat").is_file() |
27 | | - assert (test_folder_path / "output_sonata2" / "2_py_Neuron_t100.0.nrndat").is_file() |
28 | | - |
| 12 | +def test_cli_disable_reports(tmp_path): |
| 13 | + with open(SIM_DIR / CONFIG_FILE_MINI, encoding="utf-8") as fd: |
| 14 | + sim_config_data = json.load(fd) |
29 | 15 |
|
30 | | -def test_cli_disable_reports(): |
31 | | - test_folder = tempfile.TemporaryDirectory("cli-test-disable-reports") # auto removed |
32 | | - test_folder_path = Path(test_folder.name) |
33 | | - with open(SIM_DIR / CONFIG_FILE_MINI, "r") as f: |
34 | | - sim_config_data = json.load(f) |
35 | | - sim_config_data["network"] = str(SIM_DIR / CIRCUIT_DIR / "circuit_config.json") |
36 | | - with open(test_folder_path / CONFIG_FILE_MINI, "w") as f: |
37 | | - json.dump(sim_config_data, f, indent=2) |
| 16 | + sim_config_data["network"] = str(SIM_DIR / CIRCUIT_DIR / "circuit_config.json") |
| 17 | + with open(tmp_path / CONFIG_FILE_MINI, "w", encoding="utf-8") as fd: |
| 18 | + json.dump(sim_config_data, fd) |
38 | 19 |
|
39 | 20 | subprocess.run( |
40 | 21 | ["neurodamus", CONFIG_FILE_MINI, "--disable-reports"], |
41 | 22 | check=True, |
42 | 23 | capture_output=True, |
43 | | - cwd=test_folder_path |
44 | | - ) |
45 | | - # Spikes are present even if we disable reports |
46 | | - assert (test_folder_path / sim_config_data["output"]["output_dir"] / "out.h5").is_file() |
47 | | - for report in sim_config_data["reports"].keys(): |
48 | | - report_path = test_folder_path / sim_config_data["output"]["output_dir"] / (report + ".h5") |
49 | | - assert not report_path.is_file(), f"File '{report_path}' should NOT exist." |
50 | | - |
51 | | - subprocess.run( |
52 | | - ["neurodamus", CONFIG_FILE_MINI], |
53 | | - check=True, |
54 | | - capture_output=True, |
55 | | - cwd=test_folder_path |
| 24 | + cwd=tmp_path |
56 | 25 | ) |
57 | | - assert (test_folder_path / sim_config_data["output"]["output_dir"] / "out.h5").is_file() |
58 | | - for report in sim_config_data["reports"].keys(): |
59 | | - report_path = test_folder_path / sim_config_data["output"]["output_dir"] / (report + ".h5") |
60 | | - assert report_path.is_file(), f"File '{report_path}' not found." |
61 | | - |
62 | | - |
63 | | -def test_cli_keep_build(): |
64 | | - from neurodamus import Neurodamus |
65 | | - with open(SIM_DIR / CONFIG_FILE_MINI, "r") as f: |
66 | | - sim_config_data = json.load(f) |
67 | | - sim_config_data["target_simulator"] = "CORENEURON" |
68 | | - sim_config_data["output"]["output_dir"] = "output_keep_build" |
69 | | - sim_config_data["network"] = str(SIM_DIR / CIRCUIT_DIR / "circuit_config.json") |
70 | | - |
71 | | - test_folder = tempfile.TemporaryDirectory("cli-test-keep-build") # auto removed |
72 | | - test_folder_path = Path(test_folder.name) |
73 | | - with open(test_folder_path / CONFIG_FILE_MINI, "w") as f: |
74 | | - json.dump(sim_config_data, f, indent=2) |
75 | | - |
76 | | - os.chdir(test_folder_path) |
77 | | - nd = Neurodamus(CONFIG_FILE_MINI, keep_build=True, disable_reports=True) |
78 | | - nd.run() |
79 | | - coreneuron_input_dir = test_folder_path / "build" / "coreneuron_input" |
80 | | - assert coreneuron_input_dir.is_dir(), "Directory 'coreneuron_input' not found." |
81 | 26 |
|
| 27 | + sc = libsonata.SimulationConfig.from_file(CONFIG_FILE_MINI) |
| 28 | + spikes_path = tmp_path / sc.output.output_dir / "out.h5" |
82 | 29 |
|
83 | | -def test_cli_build_model(): |
84 | | - with open(SIM_DIR / CONFIG_FILE_MINI, "r") as f: |
85 | | - sim_config_data = json.load(f) |
86 | | - sim_config_data["target_simulator"] = "CORENEURON" |
87 | | - sim_config_data["network"] = str(SIM_DIR / CIRCUIT_DIR / "circuit_config.json") |
88 | | - |
89 | | - test_folder = tempfile.TemporaryDirectory("cli-test-build-model") # auto removed |
90 | | - test_folder_path = Path(test_folder.name) |
91 | | - with open(test_folder_path / CONFIG_FILE_MINI, "w") as f: |
92 | | - json.dump(sim_config_data, f, indent=2) |
93 | | - |
94 | | - result_model = subprocess.run( |
95 | | - ["neurodamus", CONFIG_FILE_MINI, "--simulate-model=OFF", "--disable-reports"], |
96 | | - check=True, |
97 | | - cwd=test_folder_path, |
98 | | - capture_output=True, |
99 | | - text=True |
100 | | - ) |
101 | | - assert "[SKIPPED] SIMULATION (MODEL BUILD ONLY)" in result_model.stdout |
102 | | - |
103 | | - result_auto = subprocess.run( |
104 | | - ["neurodamus", CONFIG_FILE_MINI, "--disable-reports"], |
105 | | - check=True, |
106 | | - cwd=test_folder_path, |
107 | | - capture_output=True, |
108 | | - text=True |
109 | | - ) |
110 | | - assert "SIMULATION (SKIP MODEL BUILD)" in result_auto.stdout |
111 | | - |
112 | | - subprocess.run( |
113 | | - ["neurodamus", CONFIG_FILE_MINI, "--simulate-model=OFF", "--disable-reports"], |
114 | | - check=True, |
115 | | - cwd=test_folder_path |
116 | | - ) |
117 | | - result_off = subprocess.run( |
118 | | - ["neurodamus", CONFIG_FILE_MINI, "--build-model=OFF", "--disable-reports"], |
119 | | - check=True, |
120 | | - cwd=test_folder_path, |
121 | | - capture_output=True, |
122 | | - text=True |
123 | | - ) |
124 | | - assert "SIMULATION (SKIP MODEL BUILD)" in result_off.stdout |
125 | | - |
126 | | - |
127 | | -def test_cli_lb_mode(): |
128 | | - test_folder = tempfile.TemporaryDirectory("cli-test-lb-mode") # auto removed |
129 | | - test_folder_path = Path(test_folder.name) |
130 | | - with open(SIM_DIR / CONFIG_FILE_MINI, "r") as f: |
131 | | - sim_config_data = json.load(f) |
132 | | - sim_config_data["network"] = str(SIM_DIR / CIRCUIT_DIR / "circuit_config.json") |
133 | | - with open(test_folder_path / CONFIG_FILE_MINI, "w") as f: |
134 | | - json.dump(sim_config_data, f, indent=2) |
135 | | - |
136 | | - for lb_mode in ("WholeCell", "MultiSplit"): |
137 | | - result = subprocess.run( |
138 | | - ["neurodamus", CONFIG_FILE_MINI, f"--lb-mode={lb_mode}", "--disable-reports"], |
139 | | - check=True, |
140 | | - cwd=test_folder_path, |
141 | | - capture_output=True, |
142 | | - text=True |
143 | | - ) |
144 | | - assert f"Load Balancing ENABLED. Mode: {lb_mode}" in result.stdout |
145 | | - assert (test_folder_path / "mcomplex.dat").is_file(), "File 'mcomplex.dat' not found." |
146 | | - assert (test_folder_path / "sim_conf").is_dir(), "Directory 'sim_conf' not found." |
147 | | - |
| 30 | + # Spikes are present even if we disable reports |
| 31 | + assert spikes_path.is_file() |
148 | 32 |
|
149 | | -def test_cli_output_path(): |
150 | | - from neurodamus import Neurodamus |
151 | | - test_folder = tempfile.TemporaryDirectory("cli-test-output-path") # auto removed |
152 | | - test_folder_path = Path(test_folder.name) |
153 | | - with open(SIM_DIR / CONFIG_FILE_MINI, "r") as f: |
154 | | - sim_config_data = json.load(f) |
155 | | - sim_config_data["network"] = str(SIM_DIR / CIRCUIT_DIR / "circuit_config.json") |
156 | | - with open(test_folder_path / CONFIG_FILE_MINI, "w") as f: |
157 | | - json.dump(sim_config_data, f, indent=2) |
| 33 | + for name in sc.list_report_names: |
| 34 | + report_path = Path(sc.report(name).file_name) |
| 35 | + assert not report_path.is_file(), f"File '{report_path}' should NOT exist." |
158 | 36 |
|
159 | | - simconfig_output_path = sim_config_data["output"]["output_dir"] |
160 | | - output_path = "new_output" |
161 | | - os.chdir(test_folder_path) |
162 | | - nd = Neurodamus(CONFIG_FILE_MINI, output_path=output_path) |
163 | | - nd.run() |
164 | | - # Output directory from simulation configuration is overridden |
165 | | - assert not (test_folder_path / simconfig_output_path).is_dir(), \ |
166 | | - f"Directory '{simconfig_output_path}' should NOT exist." |
167 | | - assert (test_folder_path / output_path).is_dir(), f"Directory '{output_path}' not found." |
| 37 | + subprocess.run(["neurodamus", CONFIG_FILE_MINI], cwd=tmp_path, check=True) |
| 38 | + assert spikes_path.is_file() |
| 39 | + for name in sc.list_report_names: |
| 40 | + report_path = Path(sc.report(name).file_name) |
| 41 | + assert report_path.is_file(), f"File '{report_path}' not found." |
0 commit comments