| Conditions | 11 |
| Total Lines | 108 |
| Code Lines | 50 |
| 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 asgardpy.gammapy.read_models.create_source_skymodel() 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 | """ |
||
| 137 | file_name = file_path.split("/")[-1] |
||
| 138 | diffuse_models_file_names_dict["gal_diffuse"] = file_name |
||
| 139 | |||
| 140 | return diffuse_models_file_names_dict |
||
| 141 | |||
| 142 | |||
| 143 | def get_target_model_from_config(source_name, asgardpy_target_config): |
||
| 144 | """ |
||
| 145 | Function to get the model of the target source from AsgardpyConfig. |
||
| 146 | """ |
||
| 147 | spectral_model = None |
||
| 148 | is_source_target = False |
||
| 149 | |||
| 150 | # If Target source model's spectral component is to be taken from Config |
||
| 151 | # and not from 3D dataset. |
||
| 152 | if asgardpy_target_config: |
||
| 153 | source_name_check = source_name.replace("_", "").replace(" ", "") |
||
| 154 | target_check = asgardpy_target_config.source_name.replace("_", "").replace(" ", "") |
||
| 155 | |||
| 156 | if source_name_check == target_check: |
||
| 157 | source_name = asgardpy_target_config.source_name |
||
| 158 | is_source_target = True |
||
| 159 | |||
| 160 | # Only taking the spectral model information right now. |
||
| 161 | if not asgardpy_target_config.from_3d: |
||
| 162 | models_ = read_models_from_asgardpy_config(asgardpy_target_config) |
||
| 163 | spectral_model = models_[0].spectral_model |
||
| 164 | |||
| 165 | return source_name, spectral_model, is_source_target |
||
| 166 | |||
| 167 | |||
| 168 | def create_source_skymodel(source_info, dl3_aux_path, base_model_type="Fermi-XML", asgardpy_target_config=None): |
||
| 169 | """ |
||
| 170 | Build SkyModels from given base model information. |
||
| 171 | |||
| 172 | If AsgardpyConfig section of the target is provided for the target |
||
| 173 | source information, it will be used to check if the target `source_name` |
||
| 174 | is provided in the base_model file. If it exists, then check if the model |
||
| 175 | information is to be read from AsgardpyConfig using `from_3d` boolean value. |
||
| 176 | Also, if EBL model information is provided in the AsgardpyConfig, it will |
||
| 177 | be added to the SkyModel object. |
||
| 178 | |||
| 179 | Parameters |
||
| 180 | ---------- |
||
| 181 | source_info: dict |
||
| 182 | Dictionary containing the source models information from XML file. |
||
| 183 | dl3_aux_path: str |
||
| 184 | Path location of the DL3 auxiliary files for reading Spatial Models |
||
| 185 | from separate files. |
||
| 186 | base_model_type: str |
||
| 187 | Name indicating the model format used to read the skymodels from. |
||
| 188 | asgardpy_target_config: `AsgardpyConfig` |
||
| 189 | Config section containing the Target source information. |
||
| 190 | |||
| 191 | Returns |
||
| 192 | ------- |
||
| 193 | source_sky_model: `gammapy.modeling.SkyModel` |
||
| 194 | SkyModels object for the given source information. |
||
| 195 | is_source_target: bool |
||
| 196 | Boolean to check if the Models belong to the target source. |
||
| 197 | """ |
||
| 198 | if base_model_type == "Fermi-XML": |
||
| 199 | source_name = source_info["@name"] |
||
| 200 | spectrum_type = source_info["spectrum"]["@type"] |
||
| 201 | spectrum_params = source_info["spectrum"]["parameter"] |
||
| 202 | |||
| 203 | # initialized to check for the case if target spectral model information |
||
| 204 | # is to be taken from the Config |
||
| 205 | spectral_model = None |
||
| 206 | |||
| 207 | # Check if target_source file exists |
||
| 208 | is_source_target = False |
||
| 209 | ebl_atten = False |
||
| 210 | |||
| 211 | source_name, spectral_model, is_source_target = get_target_model_from_config( |
||
| 212 | source_name, asgardpy_target_config |
||
| 213 | ) |
||
| 214 | |||
| 215 | if spectral_model is None: |
||
| 216 | # Define the Spectral Model type for Gammapy |
||
| 217 | spectral_model, ebl_atten = get_gammapy_spectral_model( |
||
| 218 | spectrum_type, |
||
| 219 | ebl_atten, |
||
| 220 | base_model_type, |
||
| 221 | ) |
||
| 222 | spectrum_type = spectrum_type.split("EblAtten::")[-1] |
||
| 223 | |||
| 224 | # Read the parameter values from XML file to create SpectralModel |
||
| 225 | params_list = xml_spectral_model_to_gammapy( |
||
| 226 | spectrum_params, |
||
| 227 | spectrum_type, |
||
| 228 | is_target=is_source_target, |
||
| 229 | keep_sign=ebl_atten, |
||
| 230 | base_model_type=base_model_type, |
||
| 231 | ) |
||
| 232 | |||
| 233 | for param_ in params_list: |
||
| 234 | setattr(spectral_model, param_.name, param_) |
||
| 235 | |||
| 236 | if asgardpy_target_config: |
||
| 237 | model_config = asgardpy_target_config.components[0] |
||
| 238 | ebl_absorption_included = model_config.spectral.ebl_abs.reference != "" |
||
| 239 | |||
| 240 | if is_source_target and ebl_absorption_included: |
||
| 241 | spectral_model = add_ebl_model_from_config(spectral_model, model_config) |
||
| 242 | |||
| 243 | # Reading Spatial model from the XML file |
||
| 244 | spatial_model = xml_spatial_model_to_gammapy(dl3_aux_path, source_info["spatialModel"], base_model_type) |
||
| 245 | |||
| 327 |