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