| Conditions | 11 |
| Total Lines | 80 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like check_preferred_model.main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import argparse |
||
| 80 | def main(): |
||
| 81 | args = parser.parse_args() |
||
| 82 | |||
| 83 | main_config = AsgardpyConfig.read(args.config) |
||
| 84 | config_path = Path(args.config) |
||
| 85 | config_path_file_name = config_path.name.split(".")[0] |
||
| 86 | target_source_name = main_config.target.source_name |
||
| 87 | |||
| 88 | steps_list = [] |
||
| 89 | for s in main_config.general.steps: |
||
| 90 | if s != "flux-points": |
||
| 91 | steps_list.append(s) |
||
| 92 | log.info("Target source is: %s", target_source_name) |
||
| 93 | |||
| 94 | spec_model_temp_files = get_model_config_files(["lp", "bpl", "ecpl", "pl", "eclp", "sbpl"]) |
||
| 95 | |||
| 96 | main_analysis_list, spec_models_list = fetch_all_analysis_objects( |
||
| 97 | main_config, spec_model_temp_files, args.ebl_scale_factor, args.ebl_model_name |
||
| 98 | ) |
||
| 99 | |||
| 100 | # Run Analysis Steps till Fit |
||
| 101 | PL_idx = 0 |
||
| 102 | |||
| 103 | for i, tag in enumerate(spec_models_list): |
||
| 104 | log.info("Spectral model being tested: %s", tag) |
||
| 105 | |||
| 106 | main_analysis_list[tag]["Analysis"].run(steps_list) |
||
| 107 | |||
| 108 | if tag == "pl": |
||
| 109 | PL_idx = i |
||
| 110 | |||
| 111 | fit_success_list, stat_list, dof_list, pref_over_pl_chi2_list = fetch_all_analysis_fit_info( |
||
| 112 | main_analysis_list, spec_models_list |
||
| 113 | ) |
||
| 114 | |||
| 115 | # If any spectral model has at least 5 sigmas preference over PL |
||
| 116 | best_sp_idx_lrt = np.nonzero(pref_over_pl_chi2_list == np.nanmax(pref_over_pl_chi2_list))[0] |
||
| 117 | for idx in best_sp_idx_lrt: |
||
| 118 | if pref_over_pl_chi2_list[idx] > 5: |
||
| 119 | sp_idx_lrt = idx |
||
| 120 | log.info("Best preferred spectral model over PL is %s", spec_models_list[idx]) |
||
| 121 | else: |
||
| 122 | sp_idx_lrt = PL_idx |
||
| 123 | log.info("No other model preferred over PL") |
||
| 124 | |||
| 125 | list_rel_p = check_model_preference_aic(stat_list, dof_list) |
||
| 126 | |||
| 127 | best_sp_idx_aic = np.nonzero(list_rel_p == np.nanmax(list_rel_p))[0] |
||
| 128 | |||
| 129 | for idx in best_sp_idx_aic: |
||
| 130 | if list_rel_p[idx] > 0.95: |
||
| 131 | sp_idx_aic = idx |
||
| 132 | log.info("Best preferred spectral model is %s", spec_models_list[fit_success_list][idx]) |
||
| 133 | else: |
||
| 134 | sp_idx_aic = PL_idx |
||
| 135 | log.info("No other model preferred, hence PL is selected") |
||
| 136 | |||
| 137 | stats_table = tabulate_best_fit_stats(spec_models_list, fit_success_list, main_analysis_list, list_rel_p) |
||
| 138 | |||
| 139 | stats_table.meta["Target source name"] = target_source_name |
||
| 140 | stats_table.meta["EBL model"] = args.ebl_model_name |
||
| 141 | stats_table.meta["EBL scale factor"] = args.ebl_scale_factor |
||
| 142 | |||
| 143 | file_name = f"{config_path_file_name}_{args.ebl_model_name}_{args.ebl_scale_factor}_fit_stats.ecsv" |
||
| 144 | stats_table.write( |
||
| 145 | main_config.general.outdir / file_name, |
||
| 146 | format="ascii.ecsv", |
||
| 147 | overwrite=True, |
||
| 148 | ) |
||
| 149 | |||
| 150 | if args.write_config: |
||
| 151 | log.info("Write the spectral model") |
||
| 152 | |||
| 153 | for idx, name in zip([sp_idx_lrt, sp_idx_aic], ["lrt", "aic"], strict=False): |
||
| 154 | tag = spec_models_list[fit_success_list][idx] |
||
| 155 | |||
| 156 | path = config_path.parent / f"{config_path_file_name}_model_most_pref_{name}.yaml" |
||
| 157 | |||
| 158 | yaml_ = write_output_config_yaml(main_analysis_list[tag]["Analysis"].final_model[0]) |
||
| 159 | path.write_text(yaml_) |
||
| 160 | |||
| 164 |