improver.blending.spatial_weights module

Module to adjust weights spatially based on missing data in input cubes.

class SpatiallyVaryingWeightsFromMask(blend_coord, fuzzy_length=10)[source]

Bases: BasePlugin

Plugin for adjusting weights spatially based on masked data in the input cube. It takes in an initial one dimensional cube of weights which would be used to collapse a dimension on the input cube and outputs weights which have been adjusted based on the presence of missing data in x-y slices of input data. The resulting weights cube has a x and y dimensions in addition to the one dimension in the initial cube of weights.

__init__(blend_coord, fuzzy_length=10)[source]

Initialise class.

Parameters:
  • blend_coord (str) – Coordinate over which the input 1D weights will vary

  • fuzzy_length (Union[int, float]) – Distance, in grid squares, over which the weights from the input data mask are smoothed. This is used to calculate a fuzzy scaling factor for the input weights based on how far away each grid point is from a masked point. The distance is taken as the euclidean distance from the masked point, so the fuzzy length can be a non-integer value. Any points that are at least this distance away from a masked point keep a weight of one, and any points closer than this distance to a masked point have a weight of less than one based on how close to the masked point they are.

_abc_impl = <_abc_data object>
_create_template_slice(cube_to_collapse)[source]

Create a template cube from a slice of the cube we are collapsing. The slice will be over blend_coord, y and x and will remove any other dimensions. This means that the resulting spatial weights won’t vary in any other dimension other than the blend_coord. If the mask does vary in another dimension an error is raised.

Parameters:

cube_to_collapse (Cube) – The cube that will be collapsed along the blend_coord using the spatial weights generated using this plugin. Must be masked where there is invalid data.

Return type:

Cube

Returns:

A cube with dimensions blend_coord, y, x, on which to shape the output weights cube.

Raises:
  • ValueError – if the blend coordinate is associated with more than one dimension on the cube to collapse, or no dimension

  • ValueError – if the mask on cube_to_collapse varies along a dimension other than the dimension associated with blend_coord.

_normalise_initial_weights(weights)[source]

Normalise weights so that they add up to 1 along the blend dimension at each spatial point. This is different from the normalisation that happens after the application of fuzzy smoothing near mask boundaries. Modifies weights cube in place. Array broadcasting relies on blend_coord being the leading dimension, as enforced in self._create_template_slice.

Parameters:

weights (Cube) – 3D weights containing zeros for masked points, but before fuzzy smoothing

Return type:

None

_rescale_masked_weights(weights)[source]

Apply fuzzy smoothing to weights at the edge of masked areas

Parameters:

weights (Cube) – Pre-normalised weights where the weights of masked data points have been set to 0

Return type:

Tuple[Cube, Cube]

Returns:

  • Weights where MASKED slices have been rescaled, but UNMASKED slices have not

  • Binary (0/1) map showing which weights have been rescaled

_rescale_unmasked_weights(weights, is_rescaled)[source]

Increase weights of unmasked slices at locations where masked slices have been smoothed, so that the sum of weights over self.blend_coord is re-normalised (sums to 1) at each point and the relative weightings of multiple unmasked slices are preserved. Modifies weights cube in place.

Parameters:
  • weights (Cube) – Cube of weights to which fuzzy smoothing has been applied to any masked slices

  • is_rescaled (Cube) – Cube matching weights.shape, with value of 1 where masked weights have been rescaled, and 0 where they are unchanged.

Return type:

None

process(cube_to_collapse, one_dimensional_weights_cube)[source]

Create fuzzy spatial weights based on missing data in the cube we are going to collapse and combine these with 1D weights along the blend_coord. The method is as follows:

  1. Broadcast 1D weights to the 3D shape of the input cube

  2. Set masked weights to zero. If there is no mask, return original unnormalised 1D weights with a warning message.

  3. Normalise 3D weights along the blend axis. This is needed so that a smooth spatial transition between layers can be achieved near mask boundaries.

  4. Reduce weights of masked layers near the mask boundary.

  5. Increase weights of unmasked layers near the mask boundary.

Parameters:
  • cube_to_collapse (Cube) – The cube that will be collapsed along self.blend_coord using the spatial weights generated using this plugin. Must be masked where there is invalid data. The mask may only vary along the blend and spatial coordinates, and not along any other dimensions on the cube.

  • one_dimensional_weights_cube (Cube) – A cube containing a single dimension coordinate with the same name given blend_coord. This cube contains 1D weights that will be applied along the blend_coord but need adjusting spatially based on missing data.

Return type:

Cube

Returns:

A cube containing normalised 3D spatial weights based on the cube_to_collapse mask and the one_dimensional weights supplied. Has dimensions: self.blend_coord, y, x.