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