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