malva.spacemake module¶
This code is adapted from spacemake.
- malva.spacemake.nonsingular(vmin, vmax, expander=0.001, tiny=1e-15, increasing=True)[source]¶
Modify the endpoints of a range as needed to avoid singularities.
This code was adapted from matplotlib.transforms “Copyright (c) 2012- Matplotlib Development Team; All Rights Reserved”
- Args
vmin, vmax: float, the initial endpoints. expander: float, default: 0.001, fractional amount by which vmin and
vmax are expanded if the original interval is too small, based on tiny.
- tinyfloat, default: 1e-15, hreshold for the ratio of the interval to
the maximum absolute value of its endpoints. If the interval is smaller than this, it will be expanded. This value should be around 1e-15 or larger; otherwise the interval will be approaching the double precision resolution limit.
increasing: bool, default: True, if True, swap vmin, vmax if vmin > vmax.
- Returns:
- float, endpoints, expanded and/or swapped if necessary.
If either input is inf or NaN, or if both inputs are 0 or very close to zero, it returns -expander, expander.
- Return type:
vmin, vmax
- malva.spacemake.create_mesh(width, height, diameter, distance, push_x=0, push_y=0)[source]¶
Create a mesh grid of points within a specified area, based on the given parameters.
- Parameters:
width – float, the width of the area in which to create the mesh.
height – float, the height of the area in which to create the mesh.
diameter – float, the diameter of each mesh point (used to define spacing).
distance – float, the distance between the centers of the mesh points.
push_x – float, default: 0, horizontal offset for the starting point of the mesh.
push_y – float, default: 0, vertical offset for the starting point of the mesh.
- Returns:
numpy.ndarray, matrix of shape (n_points, 2) with x, y coordinates of the mesh points.
- Return type:
xy
- malva.spacemake.binning_hexagon(x, y, gridsize, extent=None, last_row=False)[source]¶
Bins x,y points into a mesh of gridsize (across x axis, or xy axes). Points are assigned to the closest hexagon, without explicitly calculating pairwise distances.
Does not require to create a mesh beforehand, this function handles the mesh and nearest neighbor.
This code was adapted from matplotlib “Copyright (c) 2012- Matplotlib Development Team; All Rights Reserved”
- Args
x, y: numpy.ndarray, x, y spatial coordinates of points gridsize: float or tuple, the amount of hexagons in x direction (float);
y direction is automatically computed; or specified if gridsize is a tuple.
extent: tuple, of (x_min, x_max, y_min, y_max) last_row: bool, whether a last row is created in mesh or not
- Returns:
- numpy.ndarray, a (x_mesh x y_mesh) x 2 matrix, with the coordinates
(centres) of each hexagon in the binned mesh
- accumulated: list, contains the indices from the x, y arrays that were binned
to each hexagon in the mesh.
- Return type:
coordinates
- malva.spacemake.aggregate_adata_by_indices(adata, idx_to_aggregate, idx_aggregated, coordinates_aggregated=None)[source]¶
Aggregate the data (along the obs dimensions) of an AnnData object by specified indices. The size of the resulting obs has to be less or equal than the size of the input.
- Parameters:
adata – AnnData object, to be aggregated.
idx_to_aggregate – array-like, the indices of observations to aggregate.
idx_aggregated – array-like, the indices indicating how to aggregate the data.
coordinates_aggregated – Union[numpy.ndarray, None], (optional) coordinates of the aggregated spots.
- Returns:
- AnnData object, the aggregated data, with spatial coordinates updated.
The variable names are preserved from the input adata, and an additional observation field “n_joined” is included, representing the number of spots that were aggregated into each bin.
- Return type:
aggregated_adata
- malva.spacemake.create_meshed_adata(adata, px_by_um, spot_diameter_um=55, spot_distance_um=100, bead_diameter_um=10, mesh_type='circle', start_at_minimum=False, optimized_binning=True)[source]¶
Create a meshed AnnData object by binning the spatial data into a grid.
- Parameters:
adata – AnnData object, containing spatial data to be meshed.
px_by_um – float, the pixel-to-micron ratio.
spot_diameter_um – float, default: 55, the diameter of the spots in microns.
spot_distance_um – float, default: 100, the distance between the centers of the spots in microns.
bead_diameter_um – float, default: 10, the diameter of the beads in microns.
mesh_type – str, default: “circle”, the type of mesh to create (“circle” or “hexagon”).
start_at_minimum – bool, default: False, if True, the mesh grid starts at the minimum coordinate.
optimized_binning – bool, default: True, if True, use optimized binning method for creating the mesh.
- Returns:
- AnnData object, the meshed data with aggregated observations according to the specified mesh.
The spatial coordinates are updated to the mesh grid, and the original data is aggregated.
- Return type:
meshed_adata