Skip to content

Commit

Permalink
Applys results of --unsafe-fixes
Browse files Browse the repository at this point in the history
Have been manually checked locally
  • Loading branch information
CyclingNinja committed Oct 24, 2024
1 parent efdc978 commit 95bc3e7
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 123 deletions.
2 changes: 1 addition & 1 deletion ndcube/global_coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def remove(self, name):
@property
def physical_types(self):
# Docstring in GlobalCoordsABC
return dict((name, value[0]) for name, value in self._all_coords.items())
return {name: value[0] for name, value in self._all_coords.items()}

def filter_by_physical_type(self, physical_type):
"""
Expand Down
1 change: 1 addition & 0 deletions ndcube/ndcollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def aligned_dimensions(self):
return np.asanyarray(self[self._first_key].shape, dtype=object)[
np.array(self.aligned_axes[self._first_key])
]
return None

@property
def aligned_axis_physical_types(self):
Expand Down
9 changes: 4 additions & 5 deletions ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def _generate_world_coords(self, pixel_corners, wcs, needed_axes=None):
# First construct a range of pixel indices for this set of coupled dimensions
sub_range = [ranges[idx] for idx in pixel_axes_indices]
# Then get a set of non correlated dimensions
non_corr_axes = set(list(range(wcs.pixel_n_dim))) - set(pixel_axes_indices)
non_corr_axes = set(range(wcs.pixel_n_dim)) - set(pixel_axes_indices)
# And inject 0s for those coordinates
for idx in non_corr_axes:
sub_range.insert(idx, 0)
Expand Down Expand Up @@ -508,7 +508,7 @@ def axis_world_coords(self, *axes, pixel_corners=False, wcs=None):
if isinstance(wcs, ExtraCoords):
wcs = wcs.wcs
if not wcs:
return tuple()
return ()

object_names = np.array([wao_comp[0] for wao_comp in wcs.world_axis_object_components])
unique_obj_names = utils.misc.unique_sorted(object_names)
Expand Down Expand Up @@ -954,8 +954,7 @@ def __mul__(self, value):
new_data = self.data * value
new_uncertainty = (type(self.uncertainty)(self.uncertainty.array * value)
if self.uncertainty is not None else None)
new_cube = self._new_instance(data=new_data, unit=new_unit, uncertainty=new_uncertainty)
return new_cube
return self._new_instance(data=new_data, unit=new_unit, uncertainty=new_uncertainty)

def __rmul__(self, value):
return self.__mul__(value)
Expand Down Expand Up @@ -1189,7 +1188,7 @@ def my_propagate(uncertainty, data, mask, **kwargs):
# in each bin can be iterated (all bins being treated in parallel) and
# their uncertainties propagated.
bin_size = bin_shape.prod()
flat_shape = [bin_size] + list(new_shape)
flat_shape = [bin_size, *list(new_shape)]
dummy_axes = tuple(range(1, len(reshape), 2))
flat_data = np.moveaxis(reshaped_data, dummy_axes, tuple(range(naxes)))
flat_data = flat_data.reshape(flat_shape)
Expand Down
13 changes: 6 additions & 7 deletions ndcube/ndcube_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def shape(self):

@property
def _shape(self):
dimensions = [len(self.data)] + list(self.data[0].data.shape)
dimensions = [len(self.data), *list(self.data[0].data.shape)]
if len(dimensions) > 1:
# If there is a common axis, length of cube's along it may not
# be the same. Therefore if the lengths are different,
Expand All @@ -74,7 +74,7 @@ def array_axis_physical_types(self):
"""
The physical types associated with each array axis, including the sequence axis.
"""
return [("meta.obs.sequence",)] + self.data[0].array_axis_physical_types
return [("meta.obs.sequence",), *self.data[0].array_axis_physical_types]

@property
def cube_like_dimensions(self):
Expand All @@ -92,8 +92,7 @@ def cube_like_dimensions(self):
else:
cube_like_dimensions[self._common_axis] = sum(dimensions[self._common_axis + 1])
# Combine into single Quantity
cube_like_dimensions = u.Quantity(cube_like_dimensions, unit=u.pix)
return cube_like_dimensions
return u.Quantity(cube_like_dimensions, unit=u.pix)

@property
def cube_like_shape(self):
Expand Down Expand Up @@ -208,8 +207,8 @@ def sequence_axis_coords(self):
# Collect names of global coords common to all cubes.
global_names = set.intersection(*[set(cube.global_coords.keys()) for cube in self.data])
# For each coord, combine values from each cube's global coords property.
return dict([(name, [cube.global_coords[name] for cube in self.data])
for name in global_names])
return {name: [cube.global_coords[name] for cube in self.data]
for name in global_names}

def explode_along_axis(self, axis):
"""
Expand Down Expand Up @@ -500,7 +499,7 @@ def __getitem__(self, item):
# If common axis item is slice(None), result is trivial as common_axis is not changed.
if item[common_axis] == slice(None):
# Create item for slicing through the default API and slice.
return self.seq[tuple([slice(None)] + item)]
return self.seq[(slice(None), *item)]
if isinstance(item[common_axis], numbers.Integral):
# If common_axis item is an int or return an NDCube with dimensionality of N-1
sequence_index, common_axis_index = \
Expand Down
4 changes: 2 additions & 2 deletions ndcube/tests/test_global_coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_add(gc):
gc.add('name1', 'custom:physical_type1', coord1)
gc.add('name2', 'custom:physical_type2', coord2)
assert gc.keys() == {'name1', 'name2'}
assert gc.physical_types == dict((('name1', 'custom:physical_type1'), ('name2', 'custom:physical_type2')))
assert gc.physical_types == {'name1': 'custom:physical_type1', 'name2': 'custom:physical_type2'}


def test_remove(gc_coords):
Expand Down Expand Up @@ -60,7 +60,7 @@ def test_slicing(gc_coords):


def test_physical_types(gc_coords):
assert gc_coords.physical_types == dict((('name1', 'custom:physical_type1'), ('name2', 'custom:physical_type2')))
assert gc_coords.physical_types == {'name1': 'custom:physical_type1', 'name2': 'custom:physical_type2'}


def test_len(gc_coords):
Expand Down
15 changes: 7 additions & 8 deletions ndcube/tests/test_ndcollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
seq_collection = NDCollection([("seq0", sequence02), ("seq1", sequence20)], aligned_axes="all")


@pytest.mark.parametrize("item,collection,expected", [
@pytest.mark.parametrize(('item', 'collection', 'expected'), [
(0, cube_collection,
NDCollection([("cube0", cube0[:, 0]), ("cube1", cube1[:, :, 0]), ("cube2", cube2[:, 0])],
aligned_axes=((1,), (0,), (1,)))),
Expand Down Expand Up @@ -81,7 +81,7 @@ def test_collection_slicing(item, collection, expected):
helpers.assert_collections_equal(collection[item], expected)


@pytest.mark.parametrize("item,collection,expected", [("cube1", cube_collection, cube1)])
@pytest.mark.parametrize(('item', 'collection', 'expected'), [("cube1", cube_collection, cube1)])
def test_slice_cube_from_collection(item, collection, expected):
helpers.assert_cubes_equal(collection[item], expected)

Expand All @@ -91,7 +91,7 @@ def test_collection_copy():
helpers.assert_collections_equal(unaligned_collection.copy(), unaligned_collection)


@pytest.mark.parametrize("collection,popped_key,expected_popped,expected_collection", [
@pytest.mark.parametrize(('collection', 'popped_key', 'expected_popped', 'expected_collection'), [
(cube_collection, "cube0", cube0, NDCollection([("cube1", cube1), ("cube2", cube2)],
aligned_axes=aligned_axes[1:])),
(unaligned_collection, "cube0", cube0, NDCollection([("cube1", cube1), ("cube2", cube2)]))])
Expand All @@ -102,7 +102,7 @@ def test_collection_pop(collection, popped_key, expected_popped, expected_collec
helpers.assert_collections_equal(popped_collection, expected_collection)


@pytest.mark.parametrize("collection,key,expected", [
@pytest.mark.parametrize(('collection', 'key', 'expected'), [
(cube_collection, "cube0", NDCollection([("cube1", cube1), ("cube2", cube2)],
aligned_axes=aligned_axes[1:]))])
def test_del_collection(collection, key, expected):
Expand All @@ -111,7 +111,7 @@ def test_del_collection(collection, key, expected):
helpers.assert_collections_equal(del_collection, expected)


@pytest.mark.parametrize("collection,key,data,aligned_axes,expected", [
@pytest.mark.parametrize(('collection', 'key', 'data', 'aligned_axes', 'expected'), [
(cube_collection, "cube1", cube2, aligned_axes[2], NDCollection(
[("cube0", cube0), ("cube1", cube2), ("cube2", cube2)],
aligned_axes=((1, 2), (1, 2), (1, 2)))),
Expand Down Expand Up @@ -144,14 +144,14 @@ def test_collection_update_without_aligned_axes():
helpers.assert_collections_equal(orig_collection, expected)


@pytest.mark.parametrize("collection, expected_aligned_dimensions", [
@pytest.mark.parametrize(('collection', 'expected_aligned_dimensions'), [
(cube_collection, [4, 5]),
(seq_collection, [2, 3, 4, 5])])
def test_aligned_dimensions(collection, expected_aligned_dimensions):
assert np.all(collection.aligned_dimensions == expected_aligned_dimensions)


@pytest.mark.parametrize("collection, expected", [
@pytest.mark.parametrize(('collection', 'expected'), [
(cube_collection, [('custom:pos.helioprojective.lat', 'custom:pos.helioprojective.lon'),
('em.wl',)]),
(seq_collection, [('meta.obs.sequence',),
Expand All @@ -160,7 +160,6 @@ def test_aligned_dimensions(collection, expected_aligned_dimensions):
('em.wl',)])])
def test_aligned_axis_physical_types(collection, expected):
output = collection.aligned_axis_physical_types
print(output)
assert len(output) == len(expected)
for output_axis_types, expect_axis_types in zip(output, expected):
assert set(output_axis_types) == set(expect_axis_types)
56 changes: 24 additions & 32 deletions ndcube/tests/test_ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@ def test_wcs_object(all_ndcubes):
assert isinstance(all_ndcubes.wcs, BaseHighLevelWCS)


@pytest.mark.parametrize("ndc, item",
(
@pytest.mark.parametrize(("ndc", "item"),
[
("ndcube_3d_ln_lt_l", np.s_[:, :, 0]),
("ndcube_3d_ln_lt_l", np.s_[..., 0]),
("ndcube_3d_ln_lt_l", np.s_[1:2, 1:2, 0]),
("ndcube_3d_ln_lt_l", np.s_[..., 0]),
("ndcube_3d_ln_lt_l", np.s_[:, :, 0]),
("ndcube_3d_ln_lt_l", np.s_[1:2, 1:2, 0]),
("ndcube_4d_ln_lt_l_t", np.s_[:, :, 0, 0]),
("ndcube_4d_ln_lt_l_t", np.s_[..., 0, 0]),
("ndcube_4d_ln_lt_l_t", np.s_[1:2, 1:2, 1, 1]),
),
],
indirect=("ndc",))
def test_slicing_ln_lt(ndc, item):
sndc = ndc[item]
Expand All @@ -70,18 +67,15 @@ def test_slicing_ln_lt(ndc, item):
assert np.allclose(sndc.wcs.axis_correlation_matrix, np.ones(2, dtype=bool))


@pytest.mark.parametrize("ndc, item",
(
("ndcube_3d_ln_lt_l", np.s_[0, 0, :]),
("ndcube_3d_ln_lt_l", np.s_[0, 0, ...]),
("ndcube_3d_ln_lt_l", np.s_[1, 1, 1:2]),
@pytest.mark.parametrize(("ndc", "item"),
[
("ndcube_3d_ln_lt_l", np.s_[0, 0, :]),
("ndcube_3d_ln_lt_l", np.s_[0, 0, ...]),
("ndcube_3d_ln_lt_l", np.s_[1, 1, 1:2]),
("ndcube_4d_ln_lt_l_t", np.s_[0, 0, :, 0]),
("ndcube_4d_ln_lt_l_t", np.s_[0, 0, ..., 0]),
("ndcube_4d_ln_lt_l_t", np.s_[1, 1, 1:2, 1]),
),
],
indirect=("ndc",))
def test_slicing_wave(ndc, item):
sndc = ndc[item]
Expand All @@ -105,18 +99,16 @@ def test_slicing_wave(ndc, item):
assert np.allclose(sndc.wcs.axis_correlation_matrix, np.ones(1, dtype=bool))


@pytest.mark.parametrize("ndc, item",
(
@pytest.mark.parametrize(("ndc", "item"),
[
("ndcube_3d_ln_lt_l", np.s_[0, :, :]),
("ndcube_3d_ln_lt_l", np.s_[0, ...]),
("ndcube_3d_ln_lt_l", np.s_[1, 1:2]),
("ndcube_3d_ln_lt_l", np.s_[0, :, :]),
("ndcube_3d_ln_lt_l", np.s_[0, ...]),
("ndcube_3d_ln_lt_l", np.s_[1, :, 1:2]),
("ndcube_4d_ln_lt_l_t", np.s_[0, :, :, 0]),
("ndcube_4d_ln_lt_l_t", np.s_[0, ..., 0]),
("ndcube_4d_ln_lt_l_t", np.s_[1, 1:2, 1:2, 1]),
),
],
indirect=("ndc",))
def test_slicing_split_celestial(ndc, item):
sndc = ndc[item]
Expand Down Expand Up @@ -206,9 +198,9 @@ 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 == tuple()
assert awc == ()
sub_cube._generate_world_coords(pixel_corners=False, wcs=sub_cube.extra_coords)
assert awc == tuple()
assert awc == ()


@pytest.mark.xfail(reason=">1D Tables not supported")
Expand All @@ -230,7 +222,7 @@ def test_axis_world_coords_complex_ec(ndcube_4d_ln_lt_l_t):
assert u.allclose(coords[3], data)


@pytest.mark.parametrize("axes", ([-1], [2], ["em"]))
@pytest.mark.parametrize("axes", [[-1], [2], ["em"]])
def test_axis_world_coords_single(axes, ndcube_3d_ln_lt_l):
coords = ndcube_3d_ln_lt_l.axis_world_coords_values(*axes)
assert len(coords) == 1
Expand All @@ -243,7 +235,7 @@ def test_axis_world_coords_single(axes, ndcube_3d_ln_lt_l):
assert u.allclose(coords[0], [1.02e-09, 1.04e-09, 1.06e-09, 1.08e-09] * u.m)


@pytest.mark.parametrize("axes", ([-1], [2], ["em"]))
@pytest.mark.parametrize("axes", [[-1], [2], ["em"]])
def test_axis_world_coords_single_pixel_corners(axes, ndcube_3d_ln_lt_l):
coords = ndcube_3d_ln_lt_l.axis_world_coords_values(*axes, pixel_corners=True)
assert u.allclose(coords, [1.01e-09, 1.03e-09, 1.05e-09, 1.07e-09, 1.09e-09] * u.m)
Expand All @@ -252,11 +244,11 @@ def test_axis_world_coords_single_pixel_corners(axes, ndcube_3d_ln_lt_l):
assert u.allclose(coords, [1.01e-09, 1.03e-09, 1.05e-09, 1.07e-09, 1.09e-09] * u.m)


@pytest.mark.parametrize("ndc, item",
(
@pytest.mark.parametrize(("ndc", "item"),
[
("ndcube_3d_ln_lt_l", np.s_[0, 0, :]),
("ndcube_3d_ln_lt_l", np.s_[0, 0, ...]),
),
],
indirect=("ndc",))
def test_axis_world_coords_sliced_all_3d(ndc, item):
coords = ndc[item].axis_world_coords_values()
Expand All @@ -266,11 +258,11 @@ def test_axis_world_coords_sliced_all_3d(ndc, item):
assert u.allclose(coords, [1.02e-09, 1.04e-09, 1.06e-09, 1.08e-09] * u.m)


@pytest.mark.parametrize("ndc, item",
(
@pytest.mark.parametrize(("ndc", "item"),
[
("ndcube_4d_ln_lt_l_t", np.s_[0, 0, :, 0]),
("ndcube_4d_ln_lt_l_t", np.s_[0, 0, ..., 0]),
),
],
indirect=("ndc",))
def test_axis_world_coords_sliced_all_4d(ndc, item):
coords = ndc[item].axis_world_coords_values()
Expand All @@ -295,12 +287,12 @@ def test_axis_world_coords_all_4d_split(ndcube_4d_ln_l_t_lt):
1.2e-10, 1.4e-10, 1.6e-10, 1.8e-10, 2.0e-10] * u.m)


@pytest.mark.parametrize('wapt', (
@pytest.mark.parametrize('wapt', [
('custom:pos.helioprojective.lon', 'custom:pos.helioprojective.lat', 'em.wl'),
('custom:pos.helioprojective.lat', 'em.wl'),
(0, 1),
(0, 1, 3)
))
])
def test_axis_world_coords_all_4d_split_sub(ndcube_4d_ln_l_t_lt, wapt):
coords = ndcube_4d_ln_l_t_lt.axis_world_coords(*wapt)
assert len(coords) == 2
Expand Down Expand Up @@ -334,8 +326,8 @@ def test_axis_world_coords_wave(ndcube_3d_ln_lt_l):
assert u.allclose(coords[0], [1.02e-09, 1.04e-09, 1.06e-09, 1.08e-09] * u.m)


@pytest.mark.parametrize('wapt', ('custom:pos.helioprojective.lon',
'custom:pos.helioprojective.lat'))
@pytest.mark.parametrize('wapt', ['custom:pos.helioprojective.lon',
'custom:pos.helioprojective.lat'])
def test_axis_world_coords_sky(ndcube_3d_ln_lt_l, wapt):
coords = ndcube_3d_ln_lt_l.axis_world_coords(wapt)
assert len(coords) == 1
Expand Down Expand Up @@ -402,7 +394,7 @@ def test_array_axis_physical_types(ndcube_3d_ln_lt_l):
('em.wl', 'custom:PIXEL')]
output = ndcube_3d_ln_lt_l.array_axis_physical_types
for i in range(len(expected)):
assert all([physical_type in expected[i] for physical_type in output[i]])
assert all(physical_type in expected[i] for physical_type in output[i])


def test_crop(ndcube_4d_ln_lt_l_t):
Expand Down
Loading

0 comments on commit 95bc3e7

Please sign in to comment.