| Conditions | 4 |
| Total Lines | 67 |
| Code Lines | 35 |
| 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 numpy as np |
||
| 6 | def get_gnomonic_projection(figure, hpx_map, **kwargs): |
||
| 7 | """ |
||
| 8 | Returns an array containing the Gnomonic projection of the provided Healpix map. |
||
| 9 | |||
| 10 | This is equivalent to hp.gnomview of Healpy BUT the projected array is NOT plotted in the figure, so you can |
||
| 11 | plot it later on. |
||
| 12 | |||
| 13 | :param figure: a matplotlib Figure |
||
| 14 | :param hpx_map: the healpix map |
||
| 15 | :param **kwargs: keywords accepted by hp.gnomview |
||
| 16 | :return: the array containing the projection. |
||
| 17 | """ |
||
| 18 | |||
| 19 | defaults = {'coord': 'C', |
||
| 20 | 'rot': None, |
||
| 21 | 'format': '%g', |
||
| 22 | 'flip': 'astro', |
||
| 23 | 'xsize': 200, |
||
| 24 | 'ysize': None, |
||
| 25 | 'reso': 1.5, |
||
| 26 | 'nest': False, |
||
| 27 | 'min': None, |
||
| 28 | 'max': None, |
||
| 29 | 'cmap': None, |
||
| 30 | 'norm': None} |
||
| 31 | |||
| 32 | for key, default_value in defaults.items(): |
||
| 33 | |||
| 34 | if key not in kwargs: |
||
| 35 | |||
| 36 | kwargs[key] = default_value |
||
| 37 | |||
| 38 | ## Colas, 2018-07-11: The following fails for really tall figures, |
||
| 39 | ## as happens with 2D binning. Top ends up negative, probably matplotlib bug. |
||
| 40 | ## So hard code extent instead. Keep the code for now if we want to fix it. |
||
| 41 | # left, bottom, right, top = np.array(plt.gca().get_position()).ravel() |
||
| 42 | # extent = (left, bottom, right - left, top - bottom) |
||
| 43 | # margins = (0.01, 0.0, 0.0, 0.02) |
||
| 44 | # extent = (extent[0] + margins[0], |
||
| 45 | # extent[1] + margins[1], |
||
| 46 | # extent[2] - margins[2] - margins[0], |
||
| 47 | # extent[3] - margins[3] - margins[1]) |
||
| 48 | extent = (0.05, 0.05, 0.9, 0.9) |
||
| 49 | |||
| 50 | ax = PA.HpxGnomonicAxes(figure, extent, |
||
| 51 | coord=kwargs['coord'], |
||
| 52 | rot=kwargs['rot'], |
||
| 53 | format=kwargs['format'], |
||
| 54 | flipconv=kwargs['flip']) |
||
| 55 | |||
| 56 | # Suppress warnings about nans |
||
| 57 | with np.warnings.catch_warnings(): |
||
| 58 | |||
| 59 | np.warnings.filterwarnings('ignore') |
||
| 60 | |||
| 61 | img = ax.projmap(hpx_map, |
||
| 62 | nest=kwargs['nest'], |
||
| 63 | coord=kwargs['coord'], |
||
| 64 | vmin=kwargs['min'], |
||
| 65 | vmax=kwargs['max'], |
||
| 66 | xsize=kwargs['xsize'], |
||
| 67 | ysize=kwargs['ysize'], |
||
| 68 | reso=kwargs['reso'], |
||
| 69 | cmap=kwargs['cmap'], |
||
| 70 | norm=kwargs['norm']) |
||
| 71 | |||
| 72 | return img |
||
| 73 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.