Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

config.py 9.8 KB

You have to be logged in to leave a comment. Sign In
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  1. """
  2. Classes that are used to configure workflow
  3. Classes:
  4. Enums
  5. Locations
  6. """
  7. from typing import *
  8. from enum import Enum, auto
  9. from yspecies.dataset import ExpressionDataset
  10. from yspecies.preprocess import FeatureSelection
  11. from dataclasses import *
  12. class Normalize(Enum):
  13. log2 = "log2"
  14. standardize = "standardize"
  15. clr = "clr"
  16. class AnimalClass(Enum):
  17. Mammalia = "Mammalia"
  18. mammals = "Mammalia"
  19. Aves = "Aves"
  20. birds = "Aves"
  21. Reptilia = "Reptilia"
  22. reptiles = "Reptilia"
  23. Coelacanthi = "Coelacanthi"
  24. Teleostei = "Teleostei"
  25. bone_fish = "Teleostei"
  26. @staticmethod
  27. def tsv():
  28. return [cl.name.capitalize()+".tsv" for cl in AnimalClass]
  29. class Orthology(Enum):
  30. one2one = "one2one"
  31. one2many = "one2many"
  32. one2many_directed = "one2many_directed"
  33. one2oneplus_directed = "one2oneplus_directed"
  34. many2many = "many2many"
  35. all = "all"
  36. class CleaningTarget(Enum):
  37. expressions = "expressions"
  38. genes = "genes"
  39. from pathlib import Path
  40. class Locations:
  41. @property
  42. def logs(self):
  43. return self.dir / "logs"
  44. class Genes:
  45. def __init__(self, base: Path):
  46. self.dir: Path = base
  47. self.genes = self.dir
  48. self.by_class = self.dir / "by_animal_class"
  49. self.all = self.dir / "all"
  50. self.genes_meta = self.dir / "reference_genes.tsv"
  51. class Expressions:
  52. def __init__(self, base: Path):
  53. self.dir = base
  54. self.expressions = self.dir
  55. self.by_class: Path = self.dir / "by_animal_class"
  56. class Input:
  57. class Annotations:
  58. class Genage:
  59. def __init__(self, base: Path):
  60. self.dir = base
  61. self.orthologs = Locations.Genes(base / "genage_orthologs")
  62. self.conversion = self.dir / "genage_conversion.tsv"
  63. self.human = self.dir / "genage_human.tsv"
  64. self.models = self.dir / "genage_models.tsv"
  65. def __init__(self, base: Path):
  66. self.dir = base
  67. self.genage = Locations.Input.Annotations.Genage(self.dir / "genage")
  68. def __init__(self, base: Path):
  69. self.dir = base
  70. self.intput = self.dir
  71. self.genes: Locations.Genes = Locations.Genes(self.dir / "genes")
  72. self.expressions: Locations.Expressions = Locations.Expressions(self.dir / "expressions")
  73. self.species = self.dir / "species.tsv"
  74. self.samples = self.dir / "samples.tsv"
  75. self.annotations = Locations.Input.Annotations(self.dir / "annotations")
  76. class Interim:
  77. def stage(self, num: str or int):
  78. if num == "2" or num == "_2" or num == 2 or num == "two":
  79. return self.stage_two
  80. elif num == "3" or num == "_3" or num == 3 or num == "three":
  81. return self.stage_three
  82. else:
  83. return self.stage_one
  84. def __init__(self, base: Path):
  85. self.dir = base
  86. self.selected = self.dir / "selected"
  87. self.optimization = self.dir / "optimization"
  88. self.stage_one = self.dir / "stage_1"
  89. self.stage_two = self.dir / "stage_2"
  90. self.stage_three = self.dir / "stage_3"
  91. class Metrics:
  92. def __init__(self, base: Path):
  93. self.dir = base
  94. self.optimization = self.dir / "optimization"
  95. class Output:
  96. class External:
  97. def __init__(self, base: Path):
  98. self.dir: Path = base
  99. self.linear = self.dir / "linear"
  100. self.shap = self.dir / "shap"
  101. self.causal = self.dir / "causal"
  102. def __init__(self, base: Path):
  103. self.dir = base
  104. self.external = Locations.Output.External(self.dir / "external")
  105. self.intersections = self.dir / "intersections"
  106. self.stage_one = self.dir / "stage_1"
  107. self.stage_two = self.dir / "stage_2"
  108. self.plots = self.dir / "plots"
  109. def __init__(self, base: str):
  110. self.base: Path = Path(base)
  111. self.data: Path = self.base / "data"
  112. self.dir: Path = self.base / "data"
  113. self.input: Locations.Input = Locations.Input(self.dir / "input")
  114. self.interim: Locations.Interim = Locations.Interim(self.dir / "interim")
  115. self.metrics: Locations.Metrics = Locations.Metrics(self.dir / "metrics")
  116. self.output: Locations.Output = Locations.Output(self.dir / "output")
  117. class Parameters(Enum):
  118. lifespan = {"objective": "regression",
  119. 'boosting_type': 'gbdt',
  120. 'lambda_l1': 2.649670285109348,
  121. 'lambda_l2': 3.651743005278647,
  122. 'max_leaves': 21,
  123. 'max_depth': 3,
  124. 'feature_fraction': 0.7381836300988616,
  125. 'bagging_fraction': 0.5287709904685758,
  126. 'learning_rate': 0.054438364299744225,
  127. 'min_data_in_leaf': 7,
  128. 'drop_rate': 0.13171689004108006,
  129. 'metric': ['mae','mse', 'huber'],
  130. }
  131. mass_g = {"objective": "regression",
  132. 'boosting_type': 'gbdt',
  133. 'lambda_l1': 2.649670285109348,
  134. 'lambda_l2': 3.651743005278647,
  135. 'max_leaves': 21,
  136. 'max_depth': 3,
  137. 'feature_fraction': 0.7381836300988616,
  138. 'bagging_fraction': 0.5287709904685758,
  139. 'learning_rate': 0.054438364299744225,
  140. 'min_data_in_leaf': 7,
  141. 'drop_rate': 0.13171689004108006,
  142. 'metric': ['mae','mse', 'huber'],
  143. }
  144. mtGC = {"objective": "regression",
  145. 'boosting_type': 'gbdt',
  146. 'lambda_l1': 2.649670285109348,
  147. 'lambda_l2': 3.651743005278647,
  148. 'max_leaves': 21,
  149. 'max_depth': 3,
  150. 'feature_fraction': 0.7381836300988616,
  151. 'bagging_fraction': 0.5287709904685758,
  152. 'learning_rate': 0.054438364299744225,
  153. 'min_data_in_leaf': 7,
  154. 'drop_rate': 0.13171689004108006,
  155. 'metric': ['mae','mse', 'huber'],
  156. }
  157. temperature = {"objective": "regression",
  158. 'boosting_type': 'gbdt',
  159. 'lambda_l1': 2.649670285109348,
  160. 'lambda_l2': 3.651743005278647,
  161. 'max_leaves': 21,
  162. 'max_depth': 3,
  163. 'feature_fraction': 0.7381836300988616,
  164. 'bagging_fraction': 0.5287709904685758,
  165. 'learning_rate': 0.054438364299744225,
  166. 'min_data_in_leaf': 7,
  167. 'drop_rate': 0.13171689004108006,
  168. 'metric': ['mae','mse', 'huber'],
  169. }
  170. gestation = {"objective": "regression",
  171. 'boosting_type': 'gbdt',
  172. 'lambda_l1': 2.649670285109348,
  173. 'lambda_l2': 3.651743005278647,
  174. 'max_leaves': 21,
  175. 'max_depth': 3,
  176. 'feature_fraction': 0.7381836300988616,
  177. 'bagging_fraction': 0.5287709904685758,
  178. 'learning_rate': 0.054438364299744225,
  179. 'min_data_in_leaf': 7,
  180. 'drop_rate': 0.13171689004108006,
  181. 'metric': ['mae','mse', 'huber'],
  182. }
  183. metabolic_rate = {"objective": "regression",
  184. 'boosting_type': 'gbdt',
  185. 'lambda_l1': 2.649670285109348,
  186. 'lambda_l2': 3.651743005278647,
  187. 'max_leaves': 21,
  188. 'max_depth': 3,
  189. 'feature_fraction': 0.7381836300988616,
  190. 'bagging_fraction': 0.5287709904685758,
  191. 'learning_rate': 0.054438364299744225,
  192. 'min_data_in_leaf': 7,
  193. 'drop_rate': 0.13171689004108006,
  194. 'metric': ['mae','mse', 'huber'],
  195. }
  196. @dataclass(frozen=True)
  197. class DataLoader:
  198. locations: Locations
  199. selection: FeatureSelection
  200. def load_life_history(self,
  201. life_history: List[str]=["lifespan", "mass_kg", "mtGC", "metabolic_rate", "temperature", "gestation_days"],
  202. exclude_min_max: bool = True
  203. ) -> Dict[str, Tuple[ExpressionDataset, FeatureSelection]]:
  204. return OrderedDict([(trait, self.load_trait(trait)) for trait in life_history])
  205. def load_trait(self, trait: str, protected_species: Union[bool, List[str]] = True) -> Tuple[ExpressionDataset, FeatureSelection]:
  206. f = replace(self.selection, to_predict = trait)
  207. data = ExpressionDataset.from_folder(self.locations.interim.selected / trait)
  208. if isinstance(protected_species, List):
  209. return (data, replace(f, not_validated_species = protected_species))
  210. elif protected_species:
  211. return (data, replace(f, not_validated_species = data.min_max_trait(trait)))
  212. return (data, f)
  213. import optuna
  214. @dataclass
  215. class StudyLoader:
  216. locations: Locations
  217. metrics_to_improve = OrderedDict()
  218. def load_study(self, trait: str, study_filename: str = None) -> optuna.multi_objective.study.MultiObjectiveStudy:
  219. study_filename = trait if study_filename is None else study_filename
  220. url = f'sqlite:///' +str((self.locations.interim.optimization / (study_filename+".sqlite")).absolute())
  221. print('loading (if exists) study from '+url)
  222. storage = optuna.storages.RDBStorage(
  223. url=url
  224. #engine_kwargs={'check_same_thread': False}
  225. )
  226. return optuna.multi_objective.study.create_study(directions=['maximize','minimize','maximize'], storage = storage, study_name = f"{trait}_r2_huber_kendall", load_if_exists = True)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...