Coverage for tests/not_used_examples.py: 18%

49 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1""" Try to run all the scripts in the examples directory 

2 

3The plt.show() function is replaced by a dummy function to avoid showing the plots. 

4This script will skip some examples that are known to fail, see variable exclude_files. 

5When debugging, you can change variable files to a few or a single file. 

6""" 

7import glob 

8import os 

9import subprocess 

10import time 

11 

12import pytest 

13import numba 

14import numba.cuda 

15 

16@pytest.mark.slow 

17def not_examples(path_to_examples='examples'): 

18 import matplotlib 

19 import matplotlib.pyplot as plt 

20 matplotlib.get_backend() 

21 os.environ['MPLBACKEND'] = 'Agg' # Reduced warnings from 94 to 62 

22 os.environ['NUMBA_CUDA_LOW_OCCUPANCY_WARNINGS'] = '0' # Reduced warnings from 62 to 59 

23 os.environ['gamdpy_SAVE_OUTPUT_EXAMPLES'] = '0' # used to avoid file creation when running pytest 

24 matplotlib.use('Agg') # Static backend that does not halt on plt.show() 

25 # List of scripts to exclude 

26 

27 run_first = [ # These examples generate files that are used by other examples 

28 'minimal.py', 

29 'isomorph.py', 

30 'isochore.py', 

31 ] 

32 

33 exclude_files = [ 

34 'test_shear.py', 

35 # FileNotFoundError: [Errno 2] Unable to synchronously open file (unable to open file: name = 'LJ_cooled_0.70.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0) 

36 'LJchain_wall.py', # ImportError: cannot import name 'nvt_nh' from 'gamdpy.integrators' 

37 'minimal_cpu.py', 

38 ## The following are test in their own scripts 

39 'structure_factor.py', 

40 ] 

41 

42 # Save the current working directory 

43 original_cwd = os.getcwd() 

44 examples_dir = os.path.abspath(path_to_examples) 

45 

46 try: 

47 os.chdir(examples_dir) 

48 

49 # Iterate over all Python files in the examples directory 

50 files = list(glob.glob('*.py')) 

51 files.sort() 

52 

53 # Put the run_first files at the beginning 

54 for file in run_first: # Remove the files from the list if they are already there 

55 if file in files: 

56 files.remove(file) 

57 files = run_first + files 

58 

59 #files = ["calc_sq_from_h5.py", "calc_rdf_from_rumd3.py", "calc_rdf_from_h5.py"] # Uncomment and modify for debugging a few or a single file 

60 print(f"Found {len(files)} examples: {files}") 

61 print(f"Excluding {len(exclude_files)} (if present): {exclude_files}") 

62 for file in files: 

63 numba.cuda.devices.reset() 

64 if os.path.basename(file) in exclude_files: 

65 print(f"Skipping {file} (warning: may fail)") 

66 continue 

67 

68 print(f"\n\nExecuting {file}") 

69 tic = time.perf_counter() 

70 torun = subprocess.Popen(["python3", f"{file}"]) 

71 torun.wait() 

72 stdout, stderr = torun.communicate() 

73 assert torun.returncode==0, f"Example {file} failed.\n" 

74 toc = time.perf_counter() 

75 print(f"Execution time for {file}: {toc - tic:.3} s") 

76 except FileNotFoundError as e: 

77 print(f"Warning: Cannot find needed file to run {file}. Running another example may provide it.") 

78 print(f"FileNotFoundError: {e}") 

79 finally: 

80 os.chdir(original_cwd) 

81 plt.close('all') 

82 del os.environ['gamdpy_SAVE_OUTPUT_EXAMPLES'] 

83 

84 

85#if __name__ == '__main__': 

86# test_examples()