-
Notifications
You must be signed in to change notification settings - Fork 50
/
_test_all_examples.py
126 lines (102 loc) · 3.74 KB
/
_test_all_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
Test executing all examples and verifying that they run properly
:author: Michel Bierlaire
:date: Sat Jul 8 17:22:54 2023
"""
import os
import subprocess
import shutil
import unittest
import tempfile
from biogeme.parameters import biogeme_parameters
ROOT_DIR = (
'/Users/bierlair/ToBackupOnGoogleDrive/FilesOnGoogleDrive/GitHub/biogeme/examples'
)
exclude = ['replace.py']
examples_dirs = (
'swissmetro',
'swissmetro_panel',
'wtp_space',
'assisted',
'indicators',
'latent',
'latentbis',
'montecarlo',
'sampling',
)
extensions_to_clean = ['html', 'pickle', 'iter', 'log', 'pareto']
def clean_directory(directory):
file_list = os.listdir(directory)
for file in file_list:
for ext in extensions_to_clean:
if file.endswith(ext):
file_path = os.path.join(directory, file)
print(f'Remove {file_path}')
os.remove(file_path)
class ScriptExecutionTests(unittest.TestCase):
def setUp(self):
self.filename = tempfile.mktemp()
biogeme_parameters.set_value(
name='number_of_draws', value=4, section='MonteCarlo'
)
biogeme_parameters.set_value(
name='largest_neighborhood', value=2, section='AssistedSpecification'
)
biogeme_parameters.set_value(
name='maximum_attempts', value=2, section='AssistedSpecification'
)
biogeme_parameters.set_value(
name='number_of_neighbors', value=2, section='AssistedSpecification'
)
biogeme_parameters.set_value(
name='max_iterations', value=2, section='SimpleBounds'
)
biogeme_parameters.set_value(
name='bootstrap_samples', value=2, section='Estimation'
)
biogeme_parameters.set_value(
name='second_derivatives', value=0, section='SimpleBounds'
)
biogeme_parameters.dump_file(file_name=self.filename)
def tearDown(self):
if os.path.exists(self.filename):
os.remove(self.filename)
def test_script_execution(self):
# Iterate over all directories
for directory in examples_dirs:
# Clean the directory
script_dir = f'{ROOT_DIR}/{directory}'
clean_directory(script_dir)
toml_file = f'{script_dir}/biogeme.toml'
orig_file_exists = False
# Replace the file biogeme.toml, if present, with another file
if os.path.isfile(toml_file):
orig_file_exists = True
backup_file = f'{script_dir}/biogeme.toml.backup'
shutil.move(toml_file, backup_file)
shutil.copy2(self.filename, toml_file)
# Iterate over all .py in the directory
os.chdir(script_dir)
for file_name in sorted(os.listdir(script_dir)):
if file_name in exclude:
continue
file_path = os.path.join(script_dir, file_name)
# Skip directories and non-Python .py
if not os.path.isfile(file_path) or not file_name.endswith('.py'):
continue
print(f'Run {file_path}...')
# Execute the script using subprocess
try:
subprocess.check_output(
['python', file_path], stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as e:
self.fail(
f"Script {file_name} failed with error: {e.output.decode()}"
)
if orig_file_exists:
shutil.move(backup_file, toml_file)
else:
os.remove(toml_file)
if __name__ == '__main__':
unittest.main()