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

76 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-07 11:05 +0000

1import datetime 

2import json 

3import os 

4from math import pi 

5 

6from .misc import run_path, search_and_replace 

7 

8 

9class ParametersFramework: 

10 def __init__(self): 

11 self.main_path = os.getcwd() 

12 

13 def save(self, path=None): 

14 """ 

15 Save parameters in json format 

16 

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

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

19 """ 

20 if not path: 

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

22 if os.path.isfile(path): 

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

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

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

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

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

28 return path 

29 

30 @classmethod 

31 def load(cls, path): 

32 """ 

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

34 

35 :param path: path of the json parameter file 

36 :return: a Parameter object 

37 """ 

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

39 params = json.load(file) 

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

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

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

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

44 params = json.load(file) 

45 

46 param = cls() 

47 param._setattrs(params) 

48 return param 

49 

50 def _setattrs(self, dictionary): 

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

52 setattr(self, key, value) 

53 

54 def __getitem__(self, item): 

55 return getattr(self, item) 

56 

57 

58class Parameters(ParametersFramework): 

59 """ 

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

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

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

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

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

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

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

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

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

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

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

71 For example 2pi for angular values in radians. 

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

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

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

75 

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

77 :ivar n_steps: Number of training steps. 

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

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

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

81 

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

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

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

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

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

87 "mean_norm" 

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

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

90 

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

92 If it is 0 memory is allocated as needed. 

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

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

95 

96 

97 """ 

98 

99 def __init__(self): 

100 super(Parameters, self).__init__() 

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

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

103 self.periodicity = 2 * pi 

104 

105 self.learning_rate = 0.001 

106 self.n_steps = 100000 

107 self.batch_size = 256 

108 self.summary_step = 100 

109 self.checkpoint_step = 5000 

110 

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

112 self.distance_cost_scale = 500 

113 self.auto_cost_scale = 1 

114 self.auto_cost_variant = "mean_abs" 

115 self.center_cost_scale = 0.0001 

116 self.l2_reg_constant = 0.001 

117 

118 self.gpu_memory_fraction = 0 

119 self.analysis_path = "" 

120 self.id = "" 

121 

122 

123class ADCParameters(Parameters): 

124 """ 

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

126 object includes, plus the following parameters: 

127 

128 

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

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

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

132 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 

133 pairwise distance calculation. 

134 

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

136 values should be used to generate conformations (False) 

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

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

139 "mean_norm" 

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

141 

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

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

144 "mean_norm" 

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

146 (dummy) 

147 

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

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

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

151 given range 

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

153 "mean_abs", "mean_norm" 

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

155 (dummy) 

156 

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

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

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

160 

161 

162 

163 """ 

164 

165 def __init__(self): 

166 super(ADCParameters, self).__init__() 

167 

168 self.cartesian_pwd_start = None 

169 self.cartesian_pwd_stop = None 

170 self.cartesian_pwd_step = None 

171 

172 self.use_backbone_angles = False 

173 self.angle_cost_scale = 0 

174 self.angle_cost_variant = "mean_abs" 

175 self.angle_cost_reference = 1 

176 

177 self.dihedral_cost_scale = 1 

178 self.dihedral_cost_variant = "mean_abs" 

179 self.dihedral_cost_reference = 1 

180 

181 self.cartesian_cost_scale = 1 

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

183 self.cartesian_cost_variant = "mean_abs" 

184 self.cartesian_cost_reference = 1 

185 

186 self.cartesian_dist_sig_parameters = self.dist_sig_parameters 

187 self.cartesian_distance_cost_scale = 1 

188 

189 self.auto_cost_scale = None 

190 self.distance_cost_scale = None