Coverage for encodermap/encodermap_tf1/parameters.py: 18%

76 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-12-31 16:54 +0100

1# Standard Library Imports 

2import datetime 

3import json 

4import os 

5from math import pi 

6 

7# Local Folder Imports 

8from .misc import run_path, search_and_replace 

9 

10 

11class ParametersFramework: 

12 def __init__(self): 

13 self.main_path = os.getcwd() 

14 

15 def save(self, path=None): 

16 """ 

17 Save parameters in json format 

18 

19 :param path: Path where parameters should be saved. If no path is given main_path/parameters.json is used. 

20 :return: The path where the parameters were saved. 

21 """ 

22 if not path: 

23 path = os.path.join(self.main_path, "parameters.json") 

24 if os.path.isfile(path): 

25 filename, extension = os.path.splitext(path) 

26 time = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") 

27 os.rename(path, filename + "_back_" + time + extension) 

28 with open(path, "w") as file: 

29 json.dump(self.__dict__, file, indent=4, sort_keys=True) 

30 return path 

31 

32 @classmethod 

33 def load(cls, path): 

34 """ 

35 Loads the parameters saved in a json file into a new Parameter object. 

36 

37 :param path: path of the json parameter file 

38 :return: a Parameter object 

39 """ 

40 with open(path, "r") as file: 

41 params = json.load(file) 

42 if params["main_path"] != os.path.dirname(path): 

43 print("seams like the directory was moved. Parameter file is updated ...") 

44 search_and_replace(path, params["main_path"], os.path.dirname(path)) 

45 with open(path, "r") as file: 

46 params = json.load(file) 

47 

48 param = cls() 

49 param._setattrs(params) 

50 return param 

51 

52 def _setattrs(self, dictionary): 

53 for key, value in dictionary.items(): 

54 setattr(self, key, value) 

55 

56 def __getitem__(self, item): 

57 return getattr(self, item) 

58 

59 

60class Parameters(ParametersFramework): 

61 """ 

62 :ivar main_path: Defines a main path where the parameters and other things might be stored. 

63 :ivar n_neurons: List containing number of neurons for each layer up to the bottleneck layer. 

64 For example [128, 128, 2] stands for an autoencoder with the following architecture 

65 {i, 128, 128, 2, 128, 128, i} where i is the number of dimensions of the input data. 

66 :ivar activation_functions: List of activation function names as implemented in TensorFlow. 

67 For example: "relu", "tanh", "sigmoid" or "" to use no activation function. 

68 The encoder part of the network takes the activation functions from the list starting with the second element. 

69 The decoder part of the network takes the activation functions in reversed order starting with 

70 the second element form the back. For example ["", "relu", "tanh", ""] would result in a autoencoder with 

71 {"relu", "tanh", "", "tanh", "relu", ""} as sequence of activation functions. 

72 :ivar periodicity: Defines the distance between periodic walls for the inputs. 

73 For example 2pi for angular values in radians. 

74 All periodic data processed by EncoderMap must be wrapped to one periodic window. 

75 E.g. data with 2pi periodicity may contain values from -pi to pi or from 0 to 2pi. 

76 Set the periodicity to float("inf") for non-periodic inputs. 

77 

78 :ivar learning_rate: Learning rate used by the optimizer. 

79 :ivar n_steps: Number of training steps. 

80 :ivar batch_size: Number of training points used in each training step 

81 :ivar summary_step: A summary for TensorBoard is writen every summary_step steps. 

82 :ivar checkpoint_step: A checkpoint is writen every checkpoint_step steps. 

83 

84 :ivar dist_sig_parameters: Parameters for the sigmoid functions applied to the high- and low-dimensional distances 

85 in the following order (sig_h, a_h, b_h, sig_l, a_l, b_l) 

86 :ivar distance_cost_scale: Adjusts how much the distance based metric is weighted in the cost function. 

87 :ivar auto_cost_scale: Adjusts how much the autoencoding cost is weighted in the cost function. 

88 :ivar auto_cost_variant: defines how the auto cost is calculated. Must be one of: "mean_square", "mean_abs", 

89 "mean_norm" 

90 :ivar center_cost_scale: Adjusts how much the centering cost is weighted in the cost function. 

91 :ivar l2_reg_constant: Adjusts how much the l2 regularisation is weighted in the cost function. 

92 

93 :ivar gpu_memory_fraction: Specifies the fraction of gpu memory blocked. 

94 If it is 0 memory is allocated as needed. 

95 :ivar analysis_path: A path that can be used to store analysis 

96 :ivar id: Can be any name for the run. Might be useful for example for specific analysis for different data sets. 

97 

98 

99 """ 

100 

101 def __init__(self): 

102 super(Parameters, self).__init__() 

103 self.n_neurons = [128, 128, 2] 

104 self.activation_functions = ["", "tanh", "tanh", ""] 

105 self.periodicity = 2 * pi 

106 

107 self.learning_rate = 0.001 

108 self.n_steps = 100000 

109 self.batch_size = 256 

110 self.summary_step = 100 

111 self.checkpoint_step = 5000 

112 

113 self.dist_sig_parameters = (4.5, 12, 6, 1, 2, 6) 

114 self.distance_cost_scale = 500 

115 self.auto_cost_scale = 1 

116 self.auto_cost_variant = "mean_abs" 

117 self.center_cost_scale = 0.0001 

118 self.l2_reg_constant = 0.001 

119 

120 self.gpu_memory_fraction = 0 

121 self.analysis_path = "" 

122 self.id = "" 

123 

124 

125class ADCParameters(Parameters): 

126 """ 

127 This is the parameter object for the AngleDihedralCartesianEncoder. It holds all the parameters that the Parameters 

128 object includes, plus the following parameters: 

129 

130 

131 :ivar cartesian_pwd_start: index of the first atom to use for the pairwise distance calculation 

132 :ivar cartesian_pwd_stop: index of the last atom to use for the pairwise distance calculation 

133 :ivar cartesian_pwd_step: step for the calculation of paiwise distances. E.g. for a chain of atoms 

134 N-C_a-C-N-C_a-C... cartesian_pwd_start=1 and cartesian_pwd_step=3 will result in using all C-alpha atoms for the 

135 pairwise distance calculation. 

136 

137 :ivar use_backbone_angles: Allows to define whether backbone bond angles should be learned (True) or if instead mean 

138 values should be used to generate conformations (False) 

139 :ivar angle_cost_scale: Adjusts how much the angle cost is weighted in the cost function. 

140 :ivar angle_cost_variant: Defines how the angle cost is calculated. Must be one of: "mean_square", "mean_abs", 

141 "mean_norm" 

142 :ivar angle_cost_reference: Can be used to normalize the angle cost with the cost of same reference model (dummy) 

143 

144 :ivar dihedral_cost_scale: Adjusts how much the dihedral cost is weighted in the cost function. 

145 :ivar dihedral_cost_variant: Defines how the dihedral cost is calculated. Must be one of: "mean_square", "mean_abs", 

146 "mean_norm" 

147 :ivar dihedral_cost_reference: Can be used to normalize the dihedral cost with the cost of same reference model 

148 (dummy) 

149 

150 :ivar cartesian_cost_scale: Adjusts how much the cartesian cost is weighted in the cost function. 

151 :ivar cartesian_cost_scale_soft_start: Allows to slowly turn on the cartesian cost. Must be a tuple with 

152 (begin, ende) or (None, None) If begin and end are given, cartesian_cost_scale will be increased linearly in the 

153 given range 

154 :ivar cartesian_cost_variant: Defines how the cartesian cost is calculated. Must be one of: "mean_square", 

155 "mean_abs", "mean_norm" 

156 :ivar cartesian_cost_reference: Can be used to normalize the cartesian cost with the cost of same reference model 

157 (dummy) 

158 

159 :ivar cartesian_dist_sig_parameters: Parameters for the sigmoid functions applied to the high- and low-dimensional 

160 distances in the following order (sig_h, a_h, b_h, sig_l, a_l, b_l) 

161 :ivar cartesian_distance_cost_scale: Adjusts how much the cartesian distance cost is weighted in the cost function. 

162 

163 

164 

165 """ 

166 

167 def __init__(self): 

168 super(ADCParameters, self).__init__() 

169 

170 self.cartesian_pwd_start = None 

171 self.cartesian_pwd_stop = None 

172 self.cartesian_pwd_step = None 

173 

174 self.use_backbone_angles = False 

175 self.angle_cost_scale = 0 

176 self.angle_cost_variant = "mean_abs" 

177 self.angle_cost_reference = 1 

178 

179 self.dihedral_cost_scale = 1 

180 self.dihedral_cost_variant = "mean_abs" 

181 self.dihedral_cost_reference = 1 

182 

183 self.cartesian_cost_scale = 1 

184 self.cartesian_cost_scale_soft_start = (None, None) # begin, end 

185 self.cartesian_cost_variant = "mean_abs" 

186 self.cartesian_cost_reference = 1 

187 

188 self.cartesian_dist_sig_parameters = self.dist_sig_parameters 

189 self.cartesian_distance_cost_scale = 1 

190 

191 self.auto_cost_scale = None 

192 self.distance_cost_scale = None