From c4e2f42f2cc04871f80253e941d8335670df872c Mon Sep 17 00:00:00 2001 From: CyclingNinja Date: Thu, 24 Oct 2024 12:27:02 +0100 Subject: [PATCH] Adheres to line length rule --- .ruff.toml | 1 + ndcube/tests/helpers.py | 21 +++++++++---- ndcube/tests/test_ndcube.py | 43 ++++++++++++++++----------- ndcube/tests/test_ndcubesequence.py | 2 +- ndcube/utils/collection.py | 3 +- ndcube/utils/sphinx/__init__.py | 0 ndcube/utils/tests/test_utils_cube.py | 3 +- ndcube/visualization/mpl_plotter.py | 8 ++--- 8 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 ndcube/utils/sphinx/__init__.py diff --git a/.ruff.toml b/.ruff.toml index 6c1423196..775c712e9 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -49,6 +49,7 @@ extend-ignore = [ # flake8-use-pathlib "PTH123", # open() should be replaced by Path.open() # Ruff + "RUF003", # Ignore ambiguous quote marks, doesn't allow ' in comments "RUF012", # Mutable class attributes should be annotated with `typing.ClassVar` "RUF013", # PEP 484 prohibits implicit `Optional` "RUF015", # Prefer `next(iter(...))` over single element slice diff --git a/ndcube/tests/helpers.py b/ndcube/tests/helpers.py index e3c1b8ef2..766e47cf7 100644 --- a/ndcube/tests/helpers.py +++ b/ndcube/tests/helpers.py @@ -34,10 +34,17 @@ def get_hash_library_name(): Generate the hash library name for this env. """ ft2_version = f"{mpl.ft2font.__freetype_version__.replace('.', '')}" - animators_version = "dev" if (("dev" in mpl_animators.__version__) or ("rc" in mpl_animators.__version__)) else mpl_animators.__version__.replace('.', '') - mpl_version = "dev" if (("dev" in mpl.__version__) or ("rc" in mpl.__version__)) else mpl.__version__.replace('.', '') - astropy_version = "dev" if (("dev" in astropy.__version__) or ("rc" in astropy.__version__)) else astropy.__version__.replace('.', '') - return f"figure_hashes_mpl_{mpl_version}_ft_{ft2_version}_astropy_{astropy_version}_animators_{animators_version}.json" + animators_version = "dev" if (("dev" in mpl_animators.__version__) + or ("rc" in mpl_animators.__version__))\ + else mpl_animators.__version__.replace('.', '') + mpl_version = "dev" if (("dev" in mpl.__version__) + or ("rc" in mpl.__version__)) \ + else mpl.__version__.replace('.', '') + astropy_version = "dev" if (("dev" in astropy.__version__) + or ("rc" in astropy.__version__)) \ + else astropy.__version__.replace('.', '') + return (f"figure_hashes_mpl_{mpl_version}_ft_{ft2_version}" + f"_astropy_{astropy_version}_animators_{animators_version}.json") def figure_test(test_function): @@ -107,7 +114,8 @@ def assert_cubes_equal(test_input, expected_cube, check_data=True): assert np.all(test_input.shape == expected_cube.shape) assert_metas_equal(test_input.meta, expected_cube.meta) if type(test_input.extra_coords) is not type(expected_cube.extra_coords): - raise AssertionError(f"NDCube extra_coords not of same type: {type(test_input.extra_coords)} != {type(expected_cube.extra_coords)}") + raise AssertionError(f"NDCube extra_coords not of same type: " + f"{type(test_input.extra_coords)} != {type(expected_cube.extra_coords)}") if test_input.extra_coords is not None: assert_extra_coords_equal(test_input.extra_coords, expected_cube.extra_coords) @@ -147,7 +155,8 @@ def assert_wcs_are_equal(wcs1, wcs2): # SlicedLowLevelWCS vs BaseHighLevelWCS don't have the same pixel_to_world method low_level_wcs1 = wcs1.low_level_wcs if isinstance(wcs1, BaseHighLevelWCS) else wcs1 low_level_wcs2 = wcs2.low_level_wcs if isinstance(wcs2, BaseHighLevelWCS) else wcs2 - np.testing.assert_array_equal(low_level_wcs1.pixel_to_world_values(*random_idx.T), low_level_wcs2.pixel_to_world_values(*random_idx.T)) + np.testing.assert_array_equal(low_level_wcs1.pixel_to_world_values(*random_idx.T), + low_level_wcs2.pixel_to_world_values(*random_idx.T)) def create_sliced_wcs(wcs, item, dim): """ diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py index 12dd9213f..ceb12d15d 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -199,7 +199,7 @@ def test_axis_world_coords_empty_ec(ndcube_3d_l_ln_lt_ectime): # slice the cube so extra_coords is empty, and then try and run axis_world_coords awc = sub_cube.axis_world_coords(wcs=sub_cube.extra_coords) assert awc == () - sub_cube._generate_world_coords(pixel_corners=False, wcs=sub_cube.extra_coords) + sub_cube._generate_world_coords(pixel_corners=False, wcs=sub_cube.extra_coords, units=True) assert awc == () @@ -543,7 +543,7 @@ def test_crop_by_values_with_equivalent_units(ndcube_2d_ln_lt): lower_corner = [(coord[0]*u.deg).to(u.arcsec) for coord in intervals] upper_corner = [(coord[-1]*u.deg).to(u.arcsec) for coord in intervals] expected = ndcube_2d_ln_lt[0:4, 1:7] - output = ndcube_2d_ln_lt.crop_by_values(lower_corner, upper_is this locallycorner) + output = ndcube_2d_ln_lt.crop_by_values(lower_corner, upper_corner) helpers.assert_cubes_equal(output, expected) @@ -664,7 +664,7 @@ def test_crop_by_extra_coords_shared_axis(ndcube_3d_ln_lt_l_ec_sharing_axis): helpers.assert_cubes_equal(output, expected) -def test_crop_by_extra_coords_values_shared_axis(ndcube_3d_ln_is this locallylt_l_ec_sharing_axis): +def test_crop_by_extra_coords_values_shared_axis(ndcube_3d_ln_lt_l_ec_sharing_axis): cube = ndcube_3d_ln_lt_l_ec_sharing_axis lower_corner = (1 * u.m, 1 * u.keV) upper_corner = (2 * u.m, 2 * u.keV) @@ -784,7 +784,6 @@ def test_reproject_shape_out(ndcube_4d_ln_l_t_lt, wcs_4d_lt_t_l_ln): wcs_4d_lt_t_l_ln.pixel_shape = None with pytest.raises(Exception): _ = ndcube_4d_ln_l_t_lt.reproject_to(wcs_4d_lt_t_l_ln) -is this locally # should not raise an exception when shape_out is specified shape = (5, 10, 12, 8) _ = ndcube_4d_ln_l_t_lt.reproject_to(wcs_4d_lt_t_l_ln, shape_out=shape) @@ -819,11 +818,11 @@ def test_rebin(ndcube_3d_l_ln_lt_ectime): expected_uncertainty = None expected_unit = cube.unit expected_meta = cube.meta - expected_Tx = np.array([[9.99999999, 19.99999994, 29.99999979, 39.9999995, + expected_tx = np.array([[9.99999999, 19.99999994, 29.99999979, 39.9999995, 49.99999902, 59.99999831, 69.99999731, 79.99999599], [9.99999999, 19.99999994, 29.99999979, 39.9999995, 49.99999902, 59.99999831, 69.99999731, 79.99999599]]) * u.arcsec - expected_Ty = np.array([[-14.99999996, -14.9999999, -14.99999981, -14.99999969, + expected_ty = np.array([[-14.99999996, -14.9999999, -14.99999981, -14.99999969, -14.99999953, -14.99999934, -14.99999911, -14.99999885], [-4.99999999, -4.99999998, -4.99999995, -4.9999999, -4.99999985, -4.99999979, -4.99999971, -4.99999962]]) * u.arcsec @@ -836,10 +835,10 @@ def test_rebin(ndcube_3d_l_ln_lt_ectime): assert np.all(output.data == expected_data) assert np.all(output.mask == expected_mask) assert output.uncertainty == expected_uncertainty - assert output.unit == expected_unitis this locally + assert output.unit == expected_unit assert output.meta == expected_meta - assert u.allclose(output_sc.Tx, expected_Tx) - assert u.allclose(output_sc.Ty, expected_Ty) + assert u.allclose(output_sc.Tx, expected_tx) + assert u.allclose(output_sc.Ty, expected_ty) assert u.allclose(output_spec, expected_spec) assert output_time.scale == expected_time.scale assert output_time.format == expected_time.format @@ -957,13 +956,15 @@ def test_rebin_no_propagate(ndcube_2d_ln_lt_mask_uncert): bin_shape = (2, 4) cube._mask[:] = True - with pytest.warns(NDCubeUserWarning, match="Uncertainties cannot be propagated as all values are masked and operation_ignores_mask is False."): + with pytest.warns(NDCubeUserWarning, match="Uncertainties cannot be propagated as all values " + "are masked and operation_ignores_mask is False."): output = cube.rebin(bin_shape, operation=np.sum, propagate_uncertainties=True, operation_ignores_mask=False) assert output.uncertainty is None cube._mask = True - with pytest.warns(NDCubeUserWarning, match="Uncertainties cannot be propagated as all values are masked and operation_ignores_mask is False."): + with pytest.warns(NDCubeUserWarning, match="Uncertainties cannot be propagated as all values " + "are masked and operation_ignores_mask is False."): output = cube.rebin(bin_shape, operation=np.sum, propagate_uncertainties=True, operation_ignores_mask=False) assert output.uncertainty is None @@ -1103,7 +1104,7 @@ def test_cube_arithmetic_rsubtract(ndcube_2d_ln_lt_units, value): ]) def test_cube_arithmetic_multiply(ndcube_2d_ln_lt_units, value): cube_quantity = u.Quantity(ndcube_2d_ln_lt_units.data, ndcube_2d_ln_lt_units.unit) - new_cube = ndcube_2d_ln_lt_units * valueis this locally + new_cube = ndcube_2d_ln_lt_units * value check_arithmetic_value_and_units(new_cube, cube_quantity * value) # TODO: test that uncertainties scale correctly @@ -1148,7 +1149,8 @@ def test_cube_arithmetic_rdivide(ndcube_2d_ln_lt_units, value): @pytest.mark.parametrize('value', [1, 2, -1]) def test_cube_arithmetic_rdivide_uncertainty(ndcube_4d_unit_uncertainty, value): cube_quantity = u.Quantity(ndcube_4d_unit_uncertainty.data, ndcube_4d_unit_uncertainty.unit) - with pytest.warns(NDCubeUserWarning, match="UnknownUncertainty does not support uncertainty propagation with correlation. Setting uncertainties to None."): + with pytest.warns(NDCubeUserWarning, match="UnknownUncertainty does not support uncertainty " + "propagation with correlation. Setting uncertainties to None."): with np.errstate(divide='ignore'): new_cube = value / ndcube_4d_unit_uncertainty check_arithmetic_value_and_units(new_cube, value / cube_quantity) @@ -1187,7 +1189,8 @@ def test_cube_arithmetic_power(ndcube_2d_ln_lt, power): @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) def test_cube_arithmetic_power_unknown_uncertainty(ndcube_4d_unit_uncertainty, power): cube_quantity = u.Quantity(ndcube_4d_unit_uncertainty.data, ndcube_4d_unit_uncertainty.unit) - with pytest.warns(NDCubeUserWarning, match="UnknownUncertainty does not support uncertainty propagation with correlation. Setting uncertainties to None."): + with pytest.warns(NDCubeUserWarning, match="UnknownUncertainty does not support uncertainty propagation " + "with correlation. Setting uncertainties to None."): with np.errstate(divide='ignore'): new_cube = ndcube_4d_unit_uncertainty ** power check_arithmetic_value_and_units(new_cube, cube_quantity**power) @@ -1196,7 +1199,9 @@ def test_cube_arithmetic_power_unknown_uncertainty(ndcube_4d_unit_uncertainty, p @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) def test_cube_arithmetic_power_std_uncertainty(ndcube_2d_ln_lt_uncert, power): cube_quantity = u.Quantity(ndcube_2d_ln_lt_uncert.data, ndcube_2d_ln_lt_uncert.unit) - with pytest.warns(NDCubeUserWarning, match=r" does not support propagation of uncertainties for power. Setting uncertainties to None."): + with pytest.warns(NDCubeUserWarning, match=r" " + r"does not support propagation of uncertainties for power. " + r"Setting uncertainties to None."): with np.errstate(divide='ignore'): new_cube = ndcube_2d_ln_lt_uncert ** power check_arithmetic_value_and_units(new_cube, cube_quantity**power) @@ -1214,7 +1219,7 @@ def test_to(ndcube_1d_l, new_unit): def test_to_dask(ndcube_2d_dask): output = ndcube_2d_dask.to(u.mJ) - dask_type = dask.array.core.Arrayis this locally + dask_type = dask.array.core.Array assert isinstance(output.data, dask_type) assert isinstance(output.uncertainty.array, dask_type) assert isinstance(output.mask, dask_type) @@ -1230,9 +1235,11 @@ def test_squeeze(ndcube_4d_ln_l_t_lt): def test_squeeze_error(ndcube_4d_ln_l_t_lt): same = ndcube_4d_ln_l_t_lt.squeeze()[0:1,:,:,:] - with pytest.raises(ValueError, match="Cannot select any axis to squeeze out, as none of them has size equal to one."): + with pytest.raises(ValueError, match="Cannot select any axis to squeeze out, " + "as none of them has size equal to one."): same.squeeze([0,1]) - with pytest.raises(ValueError, match="All axes are of length 1, therefore we will not squeeze NDCube to become a scalar. Use `axis=` keyword to specify a subset of axes to squeeze."): + with pytest.raises(ValueError, match="All axes are of length 1, therefore we will not squeeze NDCube to become " + "a scalar. Use `axis=` keyword to specify a subset of axes to squeeze."): same[0:1,0:1,0:1,0:1].squeeze() diff --git a/ndcube/tests/test_ndcubesequence.py b/ndcube/tests/test_ndcubesequence.py index 094ccaad6..8f3a4ee4b 100644 --- a/ndcube/tests/test_ndcubesequence.py +++ b/ndcube/tests/test_ndcubesequence.py @@ -93,7 +93,7 @@ def test_index_as_cube(ndc, item, expected_shape): 4)) ], indirect=("ndc",)) -def test_explode_along_axis_common_axis_None(ndc, axis, expected_shape): +def test_explode_along_axis_common_axis_none(ndc, axis, expected_shape): exploded_sequence = ndc.explode_along_axis(axis) assert np.all(exploded_sequence.shape == expected_shape) assert exploded_sequence._common_axis is None diff --git a/ndcube/utils/collection.py b/ndcube/utils/collection.py index ac01649ba..3e3205d1e 100644 --- a/ndcube/utils/collection.py +++ b/ndcube/utils/collection.py @@ -159,5 +159,6 @@ def assert_aligned_axes_compatible(data_dimensions1, data_dimensions2, data_axes if len(data_axes1) != len(data_axes2): raise ValueError(f"Number of aligned axes must be equal: {len(data_axes1)} != {len(data_axes2)}") # Confirm dimension lengths of each aligned axis is the same. - if not all(np.array(data_dimensions1)[np.array(data_axes1)] == np.array(data_dimensions2)[np.array(data_axes2)]): + if not all(np.array(data_dimensions1)[np.array(data_axes1)] == + np.array(data_dimensions2)[np.array(data_axes2)]): raise ValueError("All corresponding aligned axes between cubes must be of same length.") diff --git a/ndcube/utils/sphinx/__init__.py b/ndcube/utils/sphinx/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/ndcube/utils/tests/test_utils_cube.py b/ndcube/utils/tests/test_utils_cube.py index 394c8c49f..5f410e435 100644 --- a/ndcube/utils/tests/test_utils_cube.py +++ b/ndcube/utils/tests/test_utils_cube.py @@ -62,7 +62,8 @@ def test_propagate_rebin_uncertainties_prod(stacked_pixel_data): # Build expected output binned_data = data.prod(axis=0) - expected = np.sqrt(((uncertainty.array / data)**2).sum(axis=0)) * binned_data / 2 # Why do I have to divide by a factor 2 here? + # TODO: Why do I have to divide by a factor 2 here? + expected = np.sqrt(((uncertainty.array / data)**2).sum(axis=0)) * binned_data / 2 expected = StdDevUncertainty(expected) # Run function diff --git a/ndcube/visualization/mpl_plotter.py b/ndcube/visualization/mpl_plotter.py index e19a07faa..9ed095ccf 100644 --- a/ndcube/visualization/mpl_plotter.py +++ b/ndcube/visualization/mpl_plotter.py @@ -79,11 +79,11 @@ def plot(self, axes=None, plot_axes=None, axes_coordinates=None, with warnings.catch_warnings(): warnings.simplefilter('ignore', AstropyUserWarning) if naxis == 1: - ax = self._plot_1D_cube(plot_wcs, axes, axes_coordinates, + ax = self._plot_1d_cube(plot_wcs, axes, axes_coordinates, axes_units, data_unit, **kwargs) elif naxis == 2 and 'y' in plot_axes: - ax = self._plot_2D_cube(plot_wcs, axes, plot_axes, axes_coordinates, + ax = self._plot_2d_cube(plot_wcs, axes, plot_axes, axes_coordinates, axes_units, data_unit, **kwargs) else: ax = self._animate_cube(plot_wcs, plot_axes=plot_axes, @@ -107,7 +107,7 @@ def _apply_axes_coordinates(self, axes, axes_coordinates): axes.coords[coord_index].set_ticks_visible(False) axes.coords[coord_index].set_ticklabel_visible(False) - def _plot_1D_cube(self, wcs, axes=None, axes_coordinates=None, axes_units=None, + def _plot_1d_cube(self, wcs, axes=None, axes_coordinates=None, axes_units=None, data_unit=None, **kwargs): if axes is None: axes = plt.subplot(projection=wcs) @@ -153,7 +153,7 @@ def _plot_1D_cube(self, wcs, axes=None, axes_coordinates=None, axes_units=None, return axes - def _plot_2D_cube(self, wcs, axes=None, plot_axes=None, axes_coordinates=None, + def _plot_2d_cube(self, wcs, axes=None, plot_axes=None, axes_coordinates=None, axes_units=None, data_unit=None, **kwargs): if axes is None: axes = plt.subplot(projection=wcs, slices=plot_axes)