| Conditions | 7 |
| Total Lines | 81 |
| Code Lines | 54 |
| 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:
| 1 | import argparse |
||
| 111 | def main(): |
||
| 112 | args = parser.parse_args() |
||
| 113 | |||
| 114 | main_config = AsgardpyConfig.read(args.config) |
||
| 115 | config_path = Path(args.config) |
||
| 116 | config_path_file_name = config_path.name.split(".")[0] |
||
| 117 | target_source_name = main_config.target.source_name |
||
| 118 | |||
| 119 | steps_list = [] |
||
| 120 | for s in main_config.general.steps: |
||
| 121 | if s != "flux-points": |
||
| 122 | steps_list.append(s) |
||
| 123 | log.info("Target source is: %s", target_source_name) |
||
| 124 | |||
| 125 | spec_model_temp_files = get_model_config_files(["lp", "bpl", "ecpl", "pl", "eclp", "sbpl"]) |
||
| 126 | |||
| 127 | main_analysis_list, spec_models_list = fetch_all_analysis_objects( |
||
| 128 | main_config, spec_model_temp_files, args.ebl_scale_factor, args.ebl_model_name |
||
| 129 | ) |
||
| 130 | |||
| 131 | # Run Analysis Steps till Fit |
||
| 132 | PL_idx = 0 |
||
| 133 | |||
| 134 | for i, tag in enumerate(spec_models_list): |
||
| 135 | log.info("Spectral model being tested: %s", tag) |
||
| 136 | |||
| 137 | main_analysis_list[tag]["Analysis"].run(steps_list) |
||
| 138 | |||
| 139 | if tag == "pl": |
||
| 140 | PL_idx = i |
||
| 141 | |||
| 142 | fit_success_list, stat_list, dof_list, pref_over_pl_chi2_list = fetch_all_analysis_fit_info( |
||
| 143 | main_analysis_list, spec_models_list |
||
| 144 | ) |
||
| 145 | |||
| 146 | # If any spectral model has at least 5 sigmas preference over PL |
||
| 147 | best_sp_idx_lrt = np.nonzero(pref_over_pl_chi2_list == np.nanmax(pref_over_pl_chi2_list))[0] |
||
| 148 | sp_idx_lrt, log = get_best_preferred_model_lrt( |
||
| 149 | best_sp_idx_lrt, |
||
| 150 | pref_over_pl_chi2_list, |
||
| 151 | spec_models_list, |
||
| 152 | PL_idx, |
||
| 153 | log, |
||
| 154 | ) |
||
| 155 | |||
| 156 | list_rel_p = check_model_preference_aic(stat_list, dof_list) |
||
| 157 | |||
| 158 | best_sp_idx_aic = np.nonzero(list_rel_p == np.nanmax(list_rel_p))[0] |
||
| 159 | |||
| 160 | sp_idx_aic, log = get_best_preferred_model_aic( |
||
| 161 | best_sp_idx_aic, |
||
| 162 | list_rel_p, |
||
| 163 | spec_models_list, |
||
| 164 | fit_success_list, |
||
| 165 | PL_idx, |
||
| 166 | log, |
||
| 167 | ) |
||
| 168 | |||
| 169 | stats_table = tabulate_best_fit_stats(spec_models_list, fit_success_list, main_analysis_list, list_rel_p) |
||
| 170 | |||
| 171 | stats_table.meta["Target source name"] = target_source_name |
||
| 172 | stats_table.meta["EBL model"] = args.ebl_model_name |
||
| 173 | stats_table.meta["EBL scale factor"] = args.ebl_scale_factor |
||
| 174 | |||
| 175 | file_name = f"{config_path_file_name}_{args.ebl_model_name}_{args.ebl_scale_factor}_fit_stats.ecsv" |
||
| 176 | stats_table.write( |
||
| 177 | main_config.general.outdir / file_name, |
||
| 178 | format="ascii.ecsv", |
||
| 179 | overwrite=True, |
||
| 180 | ) |
||
| 181 | |||
| 182 | if args.write_config: |
||
| 183 | log.info("Write the spectral model") |
||
| 184 | |||
| 185 | for idx, name in zip([sp_idx_lrt, sp_idx_aic], ["lrt", "aic"], strict=False): |
||
| 186 | tag = spec_models_list[fit_success_list][idx] |
||
| 187 | |||
| 188 | path = config_path.parent / f"{config_path_file_name}_model_most_pref_{name}.yaml" |
||
| 189 | |||
| 190 | write_asgardpy_model_to_file( |
||
| 191 | gammapy_model=main_analysis_list[tag]["Analysis"].final_model[0], output_file=path |
||
| 192 | ) |
||
| 197 |