Coverage for teaser/logic/utilities.py: 65%

48 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-04-29 16:01 +0000

1# created June 2015 

2# by TEASER4 Development Team 

3 

4"""Utilities: Collection of all utility functions that are useful in several 

5classes 

6""" 

7 

8import os 

9import shutil 

10import operator 

11 

12ops = {"/": operator.truediv} 

13 

14 

15def celsius_to_kelvin(value): 

16 try: 

17 f_value = float(value) 

18 except TypeError: 

19 f_value = 0 

20 return f_value + 273.15 

21 

22 

23def create_path(path): 

24 """Create a folder. 

25 

26 Creates a new folder. 

27 

28 Parameters 

29 ---------- 

30 path : str 

31 

32 """ 

33 path = os.path.normpath(path) 

34 # if directory exists change into that directory 

35 if(os.path.isdir(path)): 

36 return path 

37 else: 

38 if not os.path.exists(path): 

39 os.makedirs(path) 

40 return path 

41 

42 

43def get_default_path(): 

44 """Function to construct default path to OutputData folder 

45 This function constructs the default path to the OutputData folder 

46 

47 """ 

48 

49 home_path = os.path.expanduser('~') 

50 

51 teaser_default_path = os.path.join(home_path, 'TEASEROutput') 

52 

53 # directory = os.path.dirname(__file__) 

54 # src = "teaser" 

55 # last_index = directory.rfind(src) 

56 # teaser_default_path = os.path.join(directory[:last_index], "teaser", 

57 # "OutputData") 

58 

59 return teaser_default_path 

60 

61 

62def get_full_path(rel_path): 

63 """Helperfunction to construct pathes to files within teaser. 

64 

65 Parameters 

66 ---------- 

67 rel_path : str 

68 Relative path beginning from teaser source folder including 

69 filename 

70 

71 Returns 

72 ------- 

73 full_path : str 

74 

75 """ 

76 

77 directory = os.path.dirname(__file__) 

78 src = "teaser" 

79 last_index = directory.rfind(src) 

80 first_path = os.path.join(directory[:last_index], "teaser") 

81 full_path = os.path.join(first_path, rel_path) 

82 

83 return full_path 

84 

85 

86def clear_directory(dir_path=None): 

87 """Function to delete all files inside a directory. 

88 

89 Parameters 

90 ---------- 

91 dir_path : str 

92 Path of directory to be deleted. By default the teaser default 

93 directory is cleared 

94 

95 """ 

96 

97 if dir_path is None: 

98 dir_path = get_default_path() 

99 else: 

100 pass 

101 

102 if os.path.exists(dir_path): 

103 for file in os.listdir(path=dir_path): 

104 file_path = os.path.join(dir_path, file) 

105 if os.path.isfile(file_path): 

106 os.remove(os.path.join(dir_path, file)) 

107 elif os.path.isdir(file_path): 

108 shutil.rmtree(file_path) 

109 else: 

110 print('The directory path does not exist.') 

111 

112 

113def division_from_json(ordereddict): 

114 """ 

115 function to transform the output of division in json after loading to 

116 python into float values 

117 

118 Parameters 

119 ---------- 

120 ordereddict : output of the json load of arguments like 

121 "persons": {"/":[1,15]} 

122 

123 """ 

124 

125 if len(ordereddict) == 1: 

126 for op, values in ordereddict.items(): 

127 if op == '/': 

128 quotient = ops[op](values[0], values[1]) 

129 return quotient 

130 else: 

131 raise ValueError('%s not supported, only divions (/)', op) 

132 else: 

133 raise ValueError('%s has len > 1', ordereddict)