corems.encapsulation.input.parameter_from_json
1from pathlib import Path 2import dataclasses 3import json 4import tomlkit 5 6from corems.encapsulation.factory.parameters import MSParameters, LCMSParameters 7from corems.encapsulation.factory.processingSetting import ( 8 MolecularFormulaSearchSettings, 9 TransientSetting, 10 SpectralSimilaritySearchSettings, 11) 12from corems.encapsulation.factory.processingSetting import ( 13 MassSpectrumSetting, 14 DataInputSetting, 15) 16from corems.encapsulation.factory.processingSetting import ( 17 MassSpecPeakSetting, 18 GasChromatographSetting, 19 CompoundSearchSettings, 20 LCMSCollectionSettings, 21) 22 23 24def load_and_set_toml_parameters_ms(mass_spec_obj, parameters_path=False): 25 """Load parameters from a toml file and set the parameters in the mass_spec_obj 26 27 Parameters 28 ---------- 29 mass_spec_obj : MassSpectrum 30 corems MassSpectrum object 31 32 parameters_path : str, optional 33 path to the parameters file, by default False 34 35 Raises 36 ------ 37 FileNotFoundError 38 if the file is not found 39 """ 40 41 if parameters_path: 42 file_path = Path(parameters_path) 43 44 else: 45 filename = "SettingsCoreMS.toml" 46 file_path = Path.cwd() / filename 47 48 if file_path.exists(): 49 with open( 50 file_path, 51 "r", 52 encoding="utf8", 53 ) as stream: 54 data_loaded = tomlkit.load(stream) 55 _set_dict_data_ms(data_loaded, mass_spec_obj) 56 else: 57 raise FileNotFoundError("Could not locate %s", file_path) 58 59 60def load_and_set_parameters_ms(mass_spec_obj, parameters_path=False): 61 """Load parameters from a json file and set the parameters in the mass_spec_obj 62 63 Parameters 64 ---------- 65 mass_spec_obj : MassSpectrum 66 corems MassSpectrum object 67 parameters_path : str, optional 68 path to the parameters file, by default False 69 70 Raises 71 ------ 72 FileNotFoundError 73 if the file is not found 74 """ 75 76 if parameters_path: 77 file_path = Path(parameters_path) 78 79 else: 80 filename = "SettingsCoreMS.json" 81 file_path = Path.cwd() / filename 82 83 if file_path.exists(): 84 with open( 85 file_path, 86 "r", 87 encoding="utf8", 88 ) as stream: 89 data_loaded = json.load(stream) 90 _set_dict_data_ms(data_loaded, mass_spec_obj) 91 else: 92 raise FileNotFoundError("Could not locate %s", file_path) 93 94 95def load_and_set_toml_parameters_gcms(gcms_obj, parameters_path=False): 96 """Load parameters from a toml file and set the parameters in the GCMS object 97 98 Parameters 99 ---------- 100 gcms_obj : GCMSBase 101 corems GCMSBase object 102 parameters_path : str, optional 103 path to the parameters file, by default False 104 105 Raises 106 ------ 107 FileNotFoundError 108 if the file is not found 109 """ 110 111 if parameters_path: 112 file_path = Path(parameters_path) 113 114 else: 115 filename = "SettingsCoreMS.toml" 116 file_path = Path.cwd() / filename 117 118 if file_path.exists(): 119 with open( 120 file_path, 121 "r", 122 encoding="utf8", 123 ) as stream: 124 data_loaded = tomlkit.load(stream) 125 _set_dict_data_gcms(data_loaded, gcms_obj) 126 else: 127 raise FileNotFoundError("Could not locate %s", file_path) 128 129 130def load_and_set_parameters_gcms(gcms_obj, parameters_path=False): 131 """Load parameters from a json file and set the parameters in the GCMS object 132 133 Parameters 134 ---------- 135 gcms_obj : GCMSBase 136 corems GCMSBase object 137 parameters_path : str, optional 138 path to the parameters file, by default False 139 140 Raises 141 ------ 142 FileNotFoundError 143 if the file is not found 144 """ 145 146 if parameters_path: 147 file_path = Path(parameters_path) 148 149 else: 150 filename = "SettingsCoreMS.json" 151 file_path = Path.cwd() / filename 152 153 if file_path.exists(): 154 with open( 155 file_path, 156 "r", 157 encoding="utf8", 158 ) as stream: 159 data_loaded = json.load(stream) 160 _set_dict_data_gcms(data_loaded, gcms_obj) 161 else: 162 raise FileNotFoundError("Could not locate %s", file_path) 163 164 165def load_and_set_json_parameters_lcms(lcms_obj, parameters_path=False): 166 """Load parameters from a json file and set the parameters in the LCMS object 167 168 Parameters 169 ---------- 170 lcms_obj : LCMSBase 171 corems LCMSBase object 172 parameters_path : str 173 path to the parameters file saved as a .json, by default False 174 175 Raises 176 ------ 177 FileNotFoundError 178 if the file is not found 179 """ 180 181 if parameters_path: 182 file_path = Path(parameters_path) 183 184 if file_path.exists(): 185 with open( 186 file_path, 187 "r", 188 encoding="utf8", 189 ) as stream: 190 data_loaded = json.load(stream) 191 _set_dict_data_lcms(data_loaded, lcms_obj) 192 else: 193 raise FileNotFoundError("Could not locate %s", file_path) 194 195 196def load_and_set_toml_parameters_lcms(lcms_obj, parameters_path=False): 197 """Load parameters from a toml file and set the parameters in the LCMS object 198 199 Parameters 200 ---------- 201 lcms_obj : LCMSBase 202 corems LCMSBase object 203 parameters_path : str 204 path to the parameters file saved as a .toml, by default False 205 206 Raises 207 ------ 208 FileNotFoundError 209 if the file is not found 210 """ 211 212 if parameters_path: 213 file_path = Path(parameters_path) 214 215 if file_path.exists(): 216 with open( 217 file_path, 218 "r", 219 encoding="utf8", 220 ) as stream: 221 data_loaded = tomlkit.load(stream) 222 _set_dict_data_lcms(data_loaded, lcms_obj) 223 else: 224 raise FileNotFoundError("Could not locate %s", file_path) 225 226 227def _set_dict_data_gcms(data_loaded, gcms_obj): 228 """Set the parameters in the GCMS object from a dict 229 230 This function is called by load_and_set_parameters_gcms and load_and_set_toml_parameters_gcms and should not be called directly. 231 232 Parameters 233 ---------- 234 data_loaded : dict 235 dict with the parameters 236 gcms_obj : GCMSBase 237 corems GCMSBase object 238 """ 239 240 classes = [ 241 GasChromatographSetting(), 242 CompoundSearchSettings(), 243 ] 244 245 labels = ["GasChromatograph", "MolecularSearch"] 246 247 label_class = zip(labels, classes) 248 249 if data_loaded: 250 for label, classe in label_class: 251 class_data = data_loaded.get(label) 252 # not always we will not all the settings 253 # this allow a class data to be none and continue 254 # to import the other classes 255 if class_data: 256 for item, value in class_data.items(): 257 setattr(classe, item, value) 258 259 gcms_obj.chromatogram_settings = classes[0] 260 gcms_obj.molecular_search_settings = classes[1] 261 262 263def _set_dict_data_lcms(data_loaded, lcms_obj): 264 """Set the parameters on a LCMS object from a dict 265 266 This function is called by load_and_set_parameters_lcms and load_and_set_toml_parameters_lcms and should not be called directly. 267 268 Parameters 269 ---------- 270 data_loaded : dict 271 dict with the parameters 272 lcms_obj : LCMSBase 273 corems LCMSBase object 274 """ 275 276 # Load the lcms parameters 277 default_params = LCMSParameters(use_defaults=True) 278 lcms_params = data_loaded.get("LiquidChromatograph") 279 if lcms_params is not None: 280 for item, value in lcms_params.items(): 281 # If the original value is a tuple but the new one is a list we need to convert the list to a tuple 282 if isinstance(value, list) and isinstance( 283 getattr(default_params.lc_ms, item), tuple 284 ): 285 setattr(lcms_obj.parameters.lc_ms, item, tuple(value)) 286 else: 287 setattr(lcms_obj.parameters.lc_ms, item, value) 288 289 def _apply_settings_dict(param_instance, class_data): 290 if class_data is None: 291 return param_instance 292 writable = {f.name for f in dataclasses.fields(param_instance)} 293 for item, value in class_data.items(): 294 if item not in writable: 295 continue 296 if item == "usedAtoms": 297 # Build a new dict. A tomlkit table turns an assigned tuple 298 # back into an array. 299 value = {atom: tuple(atom_value) for atom, atom_value in value.items()} 300 if item == "additional_similarities" and isinstance(value, list): 301 setattr(param_instance, item, list(value)) 302 elif item == "similarity_thresholds" and isinstance(value, dict): 303 setattr(param_instance, item, dict(value)) 304 elif isinstance(value, list) and isinstance( 305 getattr(param_instance, item), tuple 306 ): 307 setattr(param_instance, item, tuple(value)) 308 else: 309 setattr(param_instance, item, value) 310 return param_instance 311 312 def set_ms_params_by_key(ms_key): 313 classes = [ 314 MassSpectrumSetting, 315 MassSpecPeakSetting, 316 MolecularFormulaSearchSettings, 317 DataInputSetting, 318 TransientSetting, 319 SpectralSimilaritySearchSettings, 320 ] 321 322 labels = [ 323 "mass_spectrum", 324 "ms_peak", 325 "molecular_search", 326 "data_input", 327 "transient", 328 "spectral_similarity_search", 329 ] 330 331 profile = data_loaded.get("mass_spectrum", {}).get(ms_key) or {} 332 label_class = zip(labels, classes) 333 334 for label, classe in label_class: 335 class_data = profile.get(label) 336 param_instance = classe() 337 param_instance = _apply_settings_dict(param_instance, class_data) 338 setattr(lcms_obj.parameters.mass_spectrum[ms_key], label, param_instance) 339 340 # Load the mass spectrum parameters 341 mass_spectrum_data = data_loaded.get("mass_spectrum") or {} 342 for ms_key in mass_spectrum_data.keys(): 343 lcms_obj.parameters.mass_spectrum[ms_key] = MSParameters() 344 set_ms_params_by_key(ms_key) 345 346 # Legacy LiquidChromatograph annotation → default ms2 profile. 347 # Only when the nested spectral_similarity_search section is absent; nested is 348 # the source of truth and must not push back onto lc_ms (keeps export/import 349 # round-trips equal for objects that only set nested fields). 350 if lcms_params is not None and "ms2" in lcms_obj.parameters.mass_spectrum: 351 profile_data = mass_spectrum_data.get("ms2") or {} 352 if "spectral_similarity_search" not in profile_data: 353 lcms_obj.parameters.sync_ms2_annotation_from_lc_ms(profile="ms2") 354 355 356def _set_dict_data_ms(data_loaded, mass_spec_obj): 357 """Set the parameters in the MassSpectrum object from a dict 358 359 This function is called by load_and_set_parameters_ms and load_and_set_toml_parameters_ms and should not be called directly. 360 361 Parameters 362 ---------- 363 data_loaded : dict 364 dict with the parameters 365 mass_spec_obj : MassSpectrum 366 corems MassSpectrum object 367 """ 368 369 370 classes = [ 371 MolecularFormulaSearchSettings(), 372 TransientSetting(), 373 MassSpectrumSetting(), 374 MassSpecPeakSetting(), 375 ] 376 377 labels = ["MolecularFormulaSearch", "Transient", "MassSpectrum", "MassSpecPeak"] 378 379 label_class = zip(labels, classes) 380 381 if data_loaded: 382 for label, classe in label_class: 383 class_data = data_loaded.get(label) 384 # not always we will have all the settings classes 385 # this allow a class data to be none and continue 386 # to import the other classes 387 if class_data: 388 for item, value in class_data.items(): 389 setattr(classe, item, value) 390 391 mass_spec_obj.molecular_search_settings = classes[0] 392 mass_spec_obj.transient_settings = classes[1] 393 mass_spec_obj.settings = classes[2] 394 mass_spec_obj.mspeaks_settings = classes[3] 395 396 397def load_and_set_toml_parameters_class( 398 parameter_label, instance_parameters_class, parameters_path=False 399): 400 """Load parameters from a toml file and set the parameters in the instance_parameters_class 401 402 Parameters 403 ---------- 404 parameter_label : str 405 label of the parameters in the toml file 406 instance_parameters_class : object 407 instance of the parameters class 408 parameters_path : str, optional 409 path to the parameters file, by default False 410 411 Raises 412 ------ 413 FileNotFoundError 414 if the file is not found 415 416 Returns 417 ------- 418 object 419 instance of the parameters class 420 """ 421 422 if parameters_path: 423 file_path = Path(parameters_path) 424 425 else: 426 file_path = Path.cwd() / "SettingsCoreMS.toml" 427 428 if file_path.exists(): 429 with open( 430 file_path, 431 "r", 432 encoding="utf8", 433 ) as stream: 434 data_loaded = tomlkit.load(stream) 435 parameter_class = _set_dict_data( 436 data_loaded, parameter_label, instance_parameters_class 437 ) 438 439 return parameter_class 440 else: 441 raise FileNotFoundError("Could not locate %s", file_path) 442 443 444def load_and_set_parameters_class( 445 parameter_label, instance_parameters_class, parameters_path=False 446): 447 """Load parameters from a json file and set the parameters in the instance_parameters_class 448 449 Parameters 450 ---------- 451 parameter_label : str 452 label of the parameters in the json file 453 instance_parameters_class : object 454 instance of the parameters class 455 parameters_path : str, optional 456 path to the parameters file, by default False 457 458 Raises 459 ------ 460 FileNotFoundError 461 if the file is not found 462 463 Returns 464 ------- 465 object 466 instance of the parameters class 467 """ 468 469 if parameters_path: 470 file_path = Path(parameters_path) 471 472 else: 473 file_path = Path.cwd() / "SettingsCoreMS.json" 474 475 if file_path.exists(): 476 with open( 477 file_path, 478 "r", 479 encoding="utf8", 480 ) as stream: 481 data_loaded = json.load(stream) 482 parameter_class = _set_dict_data( 483 data_loaded, parameter_label, instance_parameters_class 484 ) 485 486 return parameter_class 487 else: 488 raise FileNotFoundError("Could not locate %s", file_path) 489 490 491def _set_dict_data(data_loaded, parameter_label, instance_ParameterClass): 492 """Set the parameters in an instance of a parameter class from a dict 493 494 This function is called by load_and_set_parameters_class and load_and_set_toml_parameters_class and should not be called directly. 495 496 Parameters 497 ---------- 498 data_loaded : dict 499 dict with the parameters 500 parameter_label : str 501 label of the parameters in the json file 502 instance_ParameterClass : object 503 instance of the parameters class 504 505 Returns 506 ------- 507 object 508 instance of the parameters class 509 """ 510 511 classes = [instance_ParameterClass] 512 513 labels = [parameter_label] 514 515 label_class = zip(labels, classes) 516 517 if data_loaded: 518 for label, classe in label_class: 519 class_data = data_loaded.get(label) 520 # not always we will have all the settings classes 521 # this allow a class data to be none and continue 522 # to import the other classes 523 if class_data: 524 for item, value in class_data.items(): 525 setattr(classe, item, value) 526 527 return classes[0] 528 529 530def load_and_set_json_parameters_lcms_collection(lcms_collection, parameters_path): 531 """Load parameters from a json file and set the parameters in the LCMS collection object 532 533 Parameters 534 ---------- 535 lcms_collection : LCMSCollection 536 corems LCMSCollection object 537 parameters_path : str or Path 538 path to the parameters file saved as a .json 539 540 Raises 541 ------ 542 FileNotFoundError 543 if the file is not found 544 """ 545 file_path = Path(parameters_path) 546 547 if file_path.exists(): 548 with open(file_path, "r", encoding="utf8") as stream: 549 data_loaded = json.load(stream) 550 _set_dict_data_lcms_collection(data_loaded, lcms_collection) 551 else: 552 raise FileNotFoundError(f"Could not locate {file_path}") 553 554 555def load_and_set_toml_parameters_lcms_collection(lcms_collection, parameters_path): 556 """Load parameters from a toml file and set the parameters in the LCMS collection object 557 558 Parameters 559 ---------- 560 lcms_collection : LCMSCollection 561 corems LCMSCollection object 562 parameters_path : str or Path 563 path to the parameters file saved as a .toml 564 565 Raises 566 ------ 567 FileNotFoundError 568 if the file is not found 569 """ 570 file_path = Path(parameters_path) 571 572 if file_path.exists(): 573 with open(file_path, "r", encoding="utf8") as stream: 574 data_loaded = tomlkit.load(stream) 575 _set_dict_data_lcms_collection(data_loaded, lcms_collection) 576 else: 577 raise FileNotFoundError(f"Could not locate {file_path}") 578 579 580def _set_dict_data_lcms_collection(data_loaded, lcms_collection): 581 """Set the parameters in the LCMS collection object from a dict 582 583 This function is called by load_and_set_json_parameters_lcms_collection and 584 load_and_set_toml_parameters_lcms_collection and should not be called directly. 585 586 Parameters 587 ---------- 588 data_loaded : dict 589 dict with the parameters 590 lcms_collection : LCMSCollection 591 corems LCMSCollection object 592 """ 593 classes = [LCMSCollectionSettings()] 594 labels = ["LCMSCollection"] 595 596 label_class = zip(labels, classes) 597 598 if data_loaded: 599 for label, classe in label_class: 600 class_data = data_loaded.get(label) 601 # not always we will have all the settings 602 # this allows a class data to be none and continue 603 # to import the other classes 604 if class_data: 605 for attr, value in class_data.items(): 606 if hasattr(classe, attr): 607 setattr(classe, attr, value) 608 609 lcms_collection.parameters.lcms_collection = classes[0]
25def load_and_set_toml_parameters_ms(mass_spec_obj, parameters_path=False): 26 """Load parameters from a toml file and set the parameters in the mass_spec_obj 27 28 Parameters 29 ---------- 30 mass_spec_obj : MassSpectrum 31 corems MassSpectrum object 32 33 parameters_path : str, optional 34 path to the parameters file, by default False 35 36 Raises 37 ------ 38 FileNotFoundError 39 if the file is not found 40 """ 41 42 if parameters_path: 43 file_path = Path(parameters_path) 44 45 else: 46 filename = "SettingsCoreMS.toml" 47 file_path = Path.cwd() / filename 48 49 if file_path.exists(): 50 with open( 51 file_path, 52 "r", 53 encoding="utf8", 54 ) as stream: 55 data_loaded = tomlkit.load(stream) 56 _set_dict_data_ms(data_loaded, mass_spec_obj) 57 else: 58 raise FileNotFoundError("Could not locate %s", file_path)
Load parameters from a toml file and set the parameters in the mass_spec_obj
Parameters
- mass_spec_obj (MassSpectrum): corems MassSpectrum object
- parameters_path (str, optional): path to the parameters file, by default False
Raises
- FileNotFoundError: if the file is not found
61def load_and_set_parameters_ms(mass_spec_obj, parameters_path=False): 62 """Load parameters from a json file and set the parameters in the mass_spec_obj 63 64 Parameters 65 ---------- 66 mass_spec_obj : MassSpectrum 67 corems MassSpectrum object 68 parameters_path : str, optional 69 path to the parameters file, by default False 70 71 Raises 72 ------ 73 FileNotFoundError 74 if the file is not found 75 """ 76 77 if parameters_path: 78 file_path = Path(parameters_path) 79 80 else: 81 filename = "SettingsCoreMS.json" 82 file_path = Path.cwd() / filename 83 84 if file_path.exists(): 85 with open( 86 file_path, 87 "r", 88 encoding="utf8", 89 ) as stream: 90 data_loaded = json.load(stream) 91 _set_dict_data_ms(data_loaded, mass_spec_obj) 92 else: 93 raise FileNotFoundError("Could not locate %s", file_path)
Load parameters from a json file and set the parameters in the mass_spec_obj
Parameters
- mass_spec_obj (MassSpectrum): corems MassSpectrum object
- parameters_path (str, optional): path to the parameters file, by default False
Raises
- FileNotFoundError: if the file is not found
96def load_and_set_toml_parameters_gcms(gcms_obj, parameters_path=False): 97 """Load parameters from a toml file and set the parameters in the GCMS object 98 99 Parameters 100 ---------- 101 gcms_obj : GCMSBase 102 corems GCMSBase object 103 parameters_path : str, optional 104 path to the parameters file, by default False 105 106 Raises 107 ------ 108 FileNotFoundError 109 if the file is not found 110 """ 111 112 if parameters_path: 113 file_path = Path(parameters_path) 114 115 else: 116 filename = "SettingsCoreMS.toml" 117 file_path = Path.cwd() / filename 118 119 if file_path.exists(): 120 with open( 121 file_path, 122 "r", 123 encoding="utf8", 124 ) as stream: 125 data_loaded = tomlkit.load(stream) 126 _set_dict_data_gcms(data_loaded, gcms_obj) 127 else: 128 raise FileNotFoundError("Could not locate %s", file_path)
Load parameters from a toml file and set the parameters in the GCMS object
Parameters
- gcms_obj (GCMSBase): corems GCMSBase object
- parameters_path (str, optional): path to the parameters file, by default False
Raises
- FileNotFoundError: if the file is not found
131def load_and_set_parameters_gcms(gcms_obj, parameters_path=False): 132 """Load parameters from a json file and set the parameters in the GCMS object 133 134 Parameters 135 ---------- 136 gcms_obj : GCMSBase 137 corems GCMSBase object 138 parameters_path : str, optional 139 path to the parameters file, by default False 140 141 Raises 142 ------ 143 FileNotFoundError 144 if the file is not found 145 """ 146 147 if parameters_path: 148 file_path = Path(parameters_path) 149 150 else: 151 filename = "SettingsCoreMS.json" 152 file_path = Path.cwd() / filename 153 154 if file_path.exists(): 155 with open( 156 file_path, 157 "r", 158 encoding="utf8", 159 ) as stream: 160 data_loaded = json.load(stream) 161 _set_dict_data_gcms(data_loaded, gcms_obj) 162 else: 163 raise FileNotFoundError("Could not locate %s", file_path)
Load parameters from a json file and set the parameters in the GCMS object
Parameters
- gcms_obj (GCMSBase): corems GCMSBase object
- parameters_path (str, optional): path to the parameters file, by default False
Raises
- FileNotFoundError: if the file is not found
166def load_and_set_json_parameters_lcms(lcms_obj, parameters_path=False): 167 """Load parameters from a json file and set the parameters in the LCMS object 168 169 Parameters 170 ---------- 171 lcms_obj : LCMSBase 172 corems LCMSBase object 173 parameters_path : str 174 path to the parameters file saved as a .json, by default False 175 176 Raises 177 ------ 178 FileNotFoundError 179 if the file is not found 180 """ 181 182 if parameters_path: 183 file_path = Path(parameters_path) 184 185 if file_path.exists(): 186 with open( 187 file_path, 188 "r", 189 encoding="utf8", 190 ) as stream: 191 data_loaded = json.load(stream) 192 _set_dict_data_lcms(data_loaded, lcms_obj) 193 else: 194 raise FileNotFoundError("Could not locate %s", file_path)
Load parameters from a json file and set the parameters in the LCMS object
Parameters
- lcms_obj (LCMSBase): corems LCMSBase object
- parameters_path (str): path to the parameters file saved as a .json, by default False
Raises
- FileNotFoundError: if the file is not found
197def load_and_set_toml_parameters_lcms(lcms_obj, parameters_path=False): 198 """Load parameters from a toml file and set the parameters in the LCMS object 199 200 Parameters 201 ---------- 202 lcms_obj : LCMSBase 203 corems LCMSBase object 204 parameters_path : str 205 path to the parameters file saved as a .toml, by default False 206 207 Raises 208 ------ 209 FileNotFoundError 210 if the file is not found 211 """ 212 213 if parameters_path: 214 file_path = Path(parameters_path) 215 216 if file_path.exists(): 217 with open( 218 file_path, 219 "r", 220 encoding="utf8", 221 ) as stream: 222 data_loaded = tomlkit.load(stream) 223 _set_dict_data_lcms(data_loaded, lcms_obj) 224 else: 225 raise FileNotFoundError("Could not locate %s", file_path)
Load parameters from a toml file and set the parameters in the LCMS object
Parameters
- lcms_obj (LCMSBase): corems LCMSBase object
- parameters_path (str): path to the parameters file saved as a .toml, by default False
Raises
- FileNotFoundError: if the file is not found
398def load_and_set_toml_parameters_class( 399 parameter_label, instance_parameters_class, parameters_path=False 400): 401 """Load parameters from a toml file and set the parameters in the instance_parameters_class 402 403 Parameters 404 ---------- 405 parameter_label : str 406 label of the parameters in the toml file 407 instance_parameters_class : object 408 instance of the parameters class 409 parameters_path : str, optional 410 path to the parameters file, by default False 411 412 Raises 413 ------ 414 FileNotFoundError 415 if the file is not found 416 417 Returns 418 ------- 419 object 420 instance of the parameters class 421 """ 422 423 if parameters_path: 424 file_path = Path(parameters_path) 425 426 else: 427 file_path = Path.cwd() / "SettingsCoreMS.toml" 428 429 if file_path.exists(): 430 with open( 431 file_path, 432 "r", 433 encoding="utf8", 434 ) as stream: 435 data_loaded = tomlkit.load(stream) 436 parameter_class = _set_dict_data( 437 data_loaded, parameter_label, instance_parameters_class 438 ) 439 440 return parameter_class 441 else: 442 raise FileNotFoundError("Could not locate %s", file_path)
Load parameters from a toml file and set the parameters in the instance_parameters_class
Parameters
- parameter_label (str): label of the parameters in the toml file
- instance_parameters_class (object): instance of the parameters class
- parameters_path (str, optional): path to the parameters file, by default False
Raises
- FileNotFoundError: if the file is not found
Returns
- object: instance of the parameters class
445def load_and_set_parameters_class( 446 parameter_label, instance_parameters_class, parameters_path=False 447): 448 """Load parameters from a json file and set the parameters in the instance_parameters_class 449 450 Parameters 451 ---------- 452 parameter_label : str 453 label of the parameters in the json file 454 instance_parameters_class : object 455 instance of the parameters class 456 parameters_path : str, optional 457 path to the parameters file, by default False 458 459 Raises 460 ------ 461 FileNotFoundError 462 if the file is not found 463 464 Returns 465 ------- 466 object 467 instance of the parameters class 468 """ 469 470 if parameters_path: 471 file_path = Path(parameters_path) 472 473 else: 474 file_path = Path.cwd() / "SettingsCoreMS.json" 475 476 if file_path.exists(): 477 with open( 478 file_path, 479 "r", 480 encoding="utf8", 481 ) as stream: 482 data_loaded = json.load(stream) 483 parameter_class = _set_dict_data( 484 data_loaded, parameter_label, instance_parameters_class 485 ) 486 487 return parameter_class 488 else: 489 raise FileNotFoundError("Could not locate %s", file_path)
Load parameters from a json file and set the parameters in the instance_parameters_class
Parameters
- parameter_label (str): label of the parameters in the json file
- instance_parameters_class (object): instance of the parameters class
- parameters_path (str, optional): path to the parameters file, by default False
Raises
- FileNotFoundError: if the file is not found
Returns
- object: instance of the parameters class
531def load_and_set_json_parameters_lcms_collection(lcms_collection, parameters_path): 532 """Load parameters from a json file and set the parameters in the LCMS collection object 533 534 Parameters 535 ---------- 536 lcms_collection : LCMSCollection 537 corems LCMSCollection object 538 parameters_path : str or Path 539 path to the parameters file saved as a .json 540 541 Raises 542 ------ 543 FileNotFoundError 544 if the file is not found 545 """ 546 file_path = Path(parameters_path) 547 548 if file_path.exists(): 549 with open(file_path, "r", encoding="utf8") as stream: 550 data_loaded = json.load(stream) 551 _set_dict_data_lcms_collection(data_loaded, lcms_collection) 552 else: 553 raise FileNotFoundError(f"Could not locate {file_path}")
Load parameters from a json file and set the parameters in the LCMS collection object
Parameters
- lcms_collection (LCMSCollection): corems LCMSCollection object
- parameters_path (str or Path): path to the parameters file saved as a .json
Raises
- FileNotFoundError: if the file is not found
556def load_and_set_toml_parameters_lcms_collection(lcms_collection, parameters_path): 557 """Load parameters from a toml file and set the parameters in the LCMS collection object 558 559 Parameters 560 ---------- 561 lcms_collection : LCMSCollection 562 corems LCMSCollection object 563 parameters_path : str or Path 564 path to the parameters file saved as a .toml 565 566 Raises 567 ------ 568 FileNotFoundError 569 if the file is not found 570 """ 571 file_path = Path(parameters_path) 572 573 if file_path.exists(): 574 with open(file_path, "r", encoding="utf8") as stream: 575 data_loaded = tomlkit.load(stream) 576 _set_dict_data_lcms_collection(data_loaded, lcms_collection) 577 else: 578 raise FileNotFoundError(f"Could not locate {file_path}")
Load parameters from a toml file and set the parameters in the LCMS collection object
Parameters
- lcms_collection (LCMSCollection): corems LCMSCollection object
- parameters_path (str or Path): path to the parameters file saved as a .toml
Raises
- FileNotFoundError: if the file is not found