pySpacell package

pySpacell module

class pySpacell.pySpacell.Spacell(feature_file, image_label_file, column_objectnumber='ObjectNumber', column_x_y=['x', 'y'])
compute_per_image_analysis(feature_columns, method, neighborhood_matrix_type, neighborhood_p0, neighborhood_p1, **kwargs)

Computes per image spatial analysis tests for the provided features for one neighborhood matrix.

Feature_columns

list of str features’ names from feature_table. All features will be tested on the same neighborhood matrix and with the same analysis method.

Method

str can be - ‘assortativity’ or ‘ripley’ (for categorical features), - ‘moran’, ‘geary’, ‘getisord’ (global spatial autocorrelation for continuous features)

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

compute_per_object_analysis(feature_columns, method, neighborhood_matrix_type, neighborhood_p0, neighborhood_p1, **kwargs)

Computes per object spatial analysis tests for the provided features for one neighborhood matrix.

Feature_columns

list of str features’ names from feature_table. All features will be tested on the same neighborhood matrices and with the same analysis method

Method

str can be ‘moran’ or’getisord’ (local spatial autocorrelation for continuous features)

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

correlogram(feature_columns, method, neighborhood_matrix_type, neighborhood_min_p0, neighborhood_max_p1, **kwargs)

Computes a serie of spatial analysis tests for the provided features. Gives one value for the image. The starting and ending neighborhood parameters, neighborhood_p0 and neighborhood_p1, are to be set. 3 modes are available to define the intermediary neighborhood parameters.

Feature_columns

list of str features’ names from feature_table. All features will be tested on the same neighborhood matrices and with the same analysis method

Method

str can be - ‘assortativity’ or ‘ripley’ (for categorical features), - ‘moran’, ‘geary’, ‘getisord’ (global spatial autocorrelation for continuous features)

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’ Same ‘neighborhood_matrix_type’ for all the points in the correlogram.

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_step

int or float a step in terms of parameters ([p0, p0+step, p0+2step, …p1]) should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’ one of neighborhood_step, nb_pairs_step or nb_test_points should be defined.

Nb_pairs_step

int a step in terms of number of pairs for each test. Overlooked if neighborhood_step is provided.

Nb_test_points

int a number of test points. Overlooked if neighborhood_step or nb_pairs_step are provided.

get_neighborhood_matrix(neighborhood_matrix_type, neighborhood_p0, neighborhood_p1, iterations='None', pysal_object=False, **kwargs)

Return the neighborhood matrix for specified parameters if already computed, thraw a ValueError otherwise. 3 modes are available:

  1. ‘k’ for k-nearest neighbors;

  2. ‘radius’ for neighbors at an euclidean distance;

  3. ‘network’ for neighbors from the object graph (touching objects are neighbors)

For each mode, an interval is requested to know which neighbors to include.

Examples:

‘k’, 2, 4 -> 2nd, 3rd, and 4th-nearest neighbors

‘radius’, 50.75, 100.85 -> neighbors at an euclidean distance falling between 50.75 pixels and 100.85 pixels

‘network’, 0, 1 -> neighbors having boundaries touching

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’

Neighborhood_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Pysal_object

bool if True, return a pysal.lib.weights object if False, return a tuple (W, L) with W full numpy neighborhood matrix and L list of vertices’ ids

get_suffix(neighborhood_matrix_type, neighborhood_p0, neighborhood_p1, **kwargs)

Returns the suffix for the additionally computed features in the feature_table: local spatial-autocorrelation statistical tests’ outputs, and neighborhood computation’s number of neighbors for each object (‘radius’ or ‘network’ neighborhood matrix type) or distance of last neighbor (‘k’ neighborhood matrix type).

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’

Neighborhood_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

matrix_types = ['k', 'radius', 'network']
precision_float_radius = 3
class pySpacell._neighborhood_matrix.NeighborhoodMatrixComputation
compute_neighborhood_matrix(neighborhood_matrix_type, neighborhood_p0, neighborhood_p1, **kwargs)

Computes the neighborhood matrix from the label image as a pysal object. Stores it in the dataframe neighborhood_matrix_df.

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Iterations

int. Optional Only used for ‘network’ neighborhood computation mode. Number of iterations of dilation to select the object neighbors from the label image. Default value: 1

Kd_tree_approx

boolean. Optional Used for ‘radius’ and ‘k’. If set to True, then use a kd-tree to find kNN. Else compute all pair distances.

Save_neighbors

bool if True, will add a column to feature_table with the ids of neighbors if False, do not keep the ids of neighbors

class pySpacell._assortativity.Assortativity
_compute_assortativity(feature_column, neighborhood_matrix_type, neighborhood_p0, neighborhood_p1, *args, iterations='None', permutations=0, quantiles=[2.5, 97.5], **kwargs)

Computes Newman’s assortativity function. one-sided test: only look at larger assortativity than random. Stores the results in the dataframe perimage_results_table.

Feature_column

str features’ name from feature_table.

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Permutations

int number of random permutations to compute the quantiles and p-value

Quantiles

list of 2 float quantiles, 2 numbers between 0 and 100

class pySpacell._ripley.Ripley
_compute_ripley(feature_column, radius, permutations=999, quantiles=[2.5, 97.5], **kwargs)

Computes Ripley’s K cross-function. Only works with ‘radius’ neighborhood matrix type. Stores results in the dataframe perimage_results_table.

Feature_column

str features’ name from feature_table.

Radius

int or float maximum distance at which objects are considered save_neighbors

Permutations

int number of random permutations to compute the quantiles and p-value

Quantiles

list of 2 float quantiles, 2 numbers between 0 and 100

_get_edge_correction_area(radius)

Corrects the estimation for the cases where the disc is not completely inside the image. cf Protazio

class pySpacell._spatial_autocorrelation.SpatialAutocorrelation
_test_SA(feature_column, method, neighborhood_matrix_type, neighborhood_p0, neighborhood_p1, permutations=999, quantiles=[2.5, 97.5], **kwargs)

Test spatial-autocorrelation with the selected method on the selected feature from feature_table. Computes one test (per feature per image). Stores the result in the dataframe perimage_results_table. Is computed on one neighborhood matrix.

Method

str test method. Should be moran, geary or getisord

Feature_column

str features’ name from feature_table.

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Permutations

int number of random permutations to compute the quantiles and p-value

Quantiles

list of 2 float quantiles, 2 numbers between 0 and 100

_test_local_SA(feature_column, method, neighborhood_matrix_type, neighborhood_p0, neighborhood_p1, permutations=999, star=False, quantiles=[2.5, 97.5], **kwargs)

Tests feature for LOCAL/SINGLE-CELL spatial autocorrelation with the specified method. Computes one test per object. Stores the test results in the dataframe feature_table. Is computed for a given neighborhood matrix.

Method

str test method. Should be moran or getisord

Feature_column

str features’ name from feature_table.

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Permutations

int number of random permutations to compute the quantiles and p-value

Quantiles

list of 2 float quantiles, 2 numbers between 0 and 100

Star

bool Used only if method is ‘getisord’ if star=True, includes the central object in the statistics. Hot spot analysis if star=False, does not include the central object in the statistics. Outlier analysis.

class pySpacell._visualization.Visualization
get_feature_filled_image(column_to_display, with_border=True, voronoi_bool=False, **kwargs)

Fills the image with values from column_to_display.

Column_to_display

str column name from feature table with values you want to display

With_border

bool if True, will add a border of background-colored pixels between each object if False, objects will touch each other like in provided label image_label

Voronoi_bool

bool if True, computes the voronoi tessalation from the object centers if False, will color only the delimited object area provided by label image

get_hot_spots_image(feature_column, method, neighborhood_matrix_type, neighborhood_p0, neighborhood_p1, image=None, hot=True, cold=True, contours=False, **kwargs)

Plots the hot and cold spots image

Feature_column

str features’ name from feature_table.

Method

str can be - ‘assortativity’ or ‘ripley’ (for categorical features), - ‘moran’, ‘geary’, ‘getisord’ (global spatial autocorrelation for continuous features)

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’ Same ‘neighborhood_matrix_type’ for all the points in the correlogram.

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Image

None or numpy array. Optional if provided, hot/cold spots will be displayed on this image. If not, they will be displayed on the label ones. Default value: None

plot_2im(input1, input2, cmap1='plasma', cmap2='plasma', **kwargs)

Plots 2 filled images side by side.

Column_name1

str column name from feature table which contains values for the left plot

Column_name2

str column name from feature table which contains values for the right plot

Title1

str, optional title for the left plot

Title2

str, optional title for the right plot

Cmap1

str name of a matplotlib cmap, for the left plot

Cmap2

str name of a matplotlib cmap, for the right plot

Min_value1

float minimum value of the colormap for the left plot

Min_value2

float minimum value of the colormap for the right plot

Max_value1

float maximum value of the colormap for the left plot

Max_value2

float maximum value of the colormap for the right plot

plot_correlogram(feature_column, method, neighborhood_matrix_type, neighborhood_min_p0, neighborhood_max_p1, quantiles=[2.5, 97.5], **kwargs)

Plots one feature at a time. Checks if the computing has been done. Retrieves info from perimage_results_table.

Feature_column

str features’ name from feature_table.

Method

str can be - ‘assortativity’ or ‘ripley’ (for categorical features), - ‘moran’, ‘geary’, ‘getisord’ (global spatial autocorrelation for continuous features)

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’ Same ‘neighborhood_matrix_type’ for all the points in the correlogram.

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_step

int or float a step in terms of parameters ([p0, p0+step, p0+2step, …p1]) should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’ one of neighborhood_step, nb_pairs_step or nb_test_points should be defined.

Nb_pairs_step

int a step in terms of number of pairs for each test. Overlooked if neighborhood_step is provided.

Nb_test_points

int a number of test points. Overlooked if neighborhood_step or nb_pairs_step are provided.

plot_neighborhood(neighborhood_matrix_type, neighborhood_p0, neighborhood_p1, image=None, with_border=True, voronoi_bool=False, **kwargs)

Visualize neighbors on the label image (default) or on a provided image.

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Image

None or numpy array. Optional if provided, neighbors will be displayed on this image Default value: None

With_border

bool. Optional Default value: True if image not provided and if True, then display image will be label image with two pixels of background between each object if image not provided and if False, then display image will be label image

Voronoi_bool

bool. Optional. Default value: False if image not provided and if True, then display image will be the voronoi tessalation computed from label image if image not provided and if False, then display image will be the label image

plot_ripley_cross(feature_column, neighborhood_min_p0, neighborhood_max_p1, class0, class1, quantiles=[2.5, 97.5], **kwargs)

Plots one feature at a time. Checks if the computing has been done. Retrieves info from perimage_results_table.

Feature_column

str features’ name from feature_table.

Method

str can be - ‘assortativity’ or ‘ripley’ (for categorical features), - ‘moran’, ‘geary’, ‘getisord’ (global spatial autocorrelation for continuous features)

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’ Same ‘neighborhood_matrix_type’ for all the points in the correlogram.

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_step

int or float a step in terms of parameters ([p0, p0+step, p0+2step, …p1]) should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’ one of neighborhood_step, nb_pairs_step or nb_test_points should be defined.

Nb_pairs_step

int a step in terms of number of pairs for each test. Overlooked if neighborhood_step is provided.

Nb_test_points

int a number of test points. Overlooked if neighborhood_step or nb_pairs_step are provided.

plot_ripley_diff(feature_column, neighborhood_min_p0, neighborhood_max_p1, class0, class1, class2, class3, **kwargs)

Plots one feature at a time. Checks if the computing has been done. Retrieves info from perimage_results_table.

Feature_column

str features’ name from feature_table.

Method

str can be - ‘assortativity’ or ‘ripley’ (for categorical features), - ‘moran’, ‘geary’, ‘getisord’ (global spatial autocorrelation for continuous features)

Neighborhood_matrix_type

str should be ‘k’, ‘radius’, or ‘network’ Same ‘neighborhood_matrix_type’ for all the points in the correlogram.

Neighborhood_min_p0

int or float minimum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_min_p1

int or float maximum bound for the neighborhood. should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’

Neighborhood_step

int or float a step in terms of parameters ([p0, p0+step, p0+2step, …p1]) should be int for ‘k’ or ‘network’. Can be int or float for ‘radius’ one of neighborhood_step, nb_pairs_step or nb_test_points should be defined.

Nb_pairs_step

int a step in terms of number of pairs for each test. Overlooked if neighborhood_step is provided.

Nb_test_points

int a number of test points. Overlooked if neighborhood_step or nb_pairs_step are provided.

Submodule

class pySpacell._ripley.RipleyObject(classes, count_classes, K, K_cross, K_cross_star, permutations, lambda_i)
get_cross_function(class1, class2, star=True, variance_stabilization=True, **kwargs)
get_diff_cross_function(class11, class12, class21, class22, **kwargs)
get_diff_pval(class11, class12, class21, class22, star=True, **kwargs)
get_diff_quantiles(class11, class12, class21, class22, star=True, quantiles=[2.5, 97.5], variance_stabilization=True, **kwargs)
get_pval(class1, class2, star=True, **kwargs)
get_quantiles(class1, class2, star=True, quantiles=[2.5, 97.5], variance_stabilization=True, **kwargs)