Title: | Generalized Application of Co-Occurrence Matrices and Haralick Texture |
---|---|
Description: | Generalizes application of gray-level co-occurrence matrix (GLCM) metrics to objects outside of images. The current focus is to apply GLCM metrics to the study of biological networks and fitness landscapes that are used in studying evolutionary medicine and biology, particularly the evolution of cancer resistance. The package was used in our publication, Barker-Clarke et al. (2023) <doi:10.1088/1361-6560/ace305>. A general reference to learn more about mathematical oncology can be found at Rockne et al. (2019) <doi:10.1088/1478-3975/ab1a09>. |
Authors: | Rowan Barker-Clarke [aut, cre]
|
Maintainer: | Rowan Barker-Clarke <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2025-03-05 04:34:07 UTC |
Source: | https://github.com/rbarkerclarke/gtexture |
Calculate the autocorrelation feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
autocorrelation(x, ...) ## Default S3 method: autocorrelation(x, ...) ## S3 method for class 'matrix' autocorrelation(x, ...) ## S3 method for class 'FitLandDF' autocorrelation(x, nlevels, ...)
autocorrelation(x, ...) ## Default S3 method: autocorrelation(x, ...) ## S3 method for class 'matrix' autocorrelation(x, ...) ## S3 method for class 'FitLandDF' autocorrelation(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
autocorrelation metric of x
## calculate autocorrelation of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate autocorrelation autocorrelation(n_x) ## calculate autocorrelation of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate autocorrelation of fitness landscape, assuming 2 discrete gray levels autocorrelation(my_landscape, nlevels = 2) ## confirm value of autocorrelation for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate autocorrelation of extracted GLCM autocorrelation(my_glcm) # should match value of above autocorrelation function call
## calculate autocorrelation of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate autocorrelation autocorrelation(n_x) ## calculate autocorrelation of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate autocorrelation of fitness landscape, assuming 2 discrete gray levels autocorrelation(my_landscape, nlevels = 2) ## confirm value of autocorrelation for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate autocorrelation of extracted GLCM autocorrelation(my_glcm) # should match value of above autocorrelation function call
Calculate the cluster prominence feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
cluster_prom(x, ...) ## Default S3 method: cluster_prom(x, ...) ## S3 method for class 'matrix' cluster_prom(x, ...) ## S3 method for class 'FitLandDF' cluster_prom(x, nlevels, ...)
cluster_prom(x, ...) ## Default S3 method: cluster_prom(x, ...) ## S3 method for class 'matrix' cluster_prom(x, ...) ## S3 method for class 'FitLandDF' cluster_prom(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
cluster prominence metric of x
## calculate cluster prominence of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate cluster prominence cluster_prom(n_x) ## calculate cluster prominence of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate cluster prominence of fitness landscape, assuming 2 discrete gray levels cluster_prom(my_landscape, nlevels = 2) ## confirm value of cluster prominence for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate cluster prominence of extracted GLCM cluster_prom(my_glcm) # should match value of above cluster_prom function call
## calculate cluster prominence of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate cluster prominence cluster_prom(n_x) ## calculate cluster prominence of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate cluster prominence of fitness landscape, assuming 2 discrete gray levels cluster_prom(my_landscape, nlevels = 2) ## confirm value of cluster prominence for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate cluster prominence of extracted GLCM cluster_prom(my_glcm) # should match value of above cluster_prom function call
Calculate the cluster shade feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
cluster_shade(x, ...) ## Default S3 method: cluster_shade(x, ...) ## S3 method for class 'matrix' cluster_shade(x, ...) ## S3 method for class 'FitLandDF' cluster_shade(x, nlevels, ...)
cluster_shade(x, ...) ## Default S3 method: cluster_shade(x, ...) ## S3 method for class 'matrix' cluster_shade(x, ...) ## S3 method for class 'FitLandDF' cluster_shade(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
cluster shade metric of x
## calculate cluster shade of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate cluster shade cluster_shade(n_x) ## calculate cluster shade of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate cluster shade of fitness landscape, assuming 2 discrete gray levels cluster_shade(my_landscape, nlevels = 2) ## confirm value of cluster shade for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate cluster shade of extracted GLCM cluster_shade(my_glcm) # should match value of above cluster_shade function call
## calculate cluster shade of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate cluster shade cluster_shade(n_x) ## calculate cluster shade of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate cluster shade of fitness landscape, assuming 2 discrete gray levels cluster_shade(my_landscape, nlevels = 2) ## confirm value of cluster shade for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate cluster shade of extracted GLCM cluster_shade(my_glcm) # should match value of above cluster_shade function call
Calculate generalized co-occurrence matrix from a variety of objects,
currently including fitness landscapes stored as a FitLandDF
instance from
the fitscape
package.
get_comatrix(x, ...) ## Default S3 method: get_comatrix(x, ...) ## S3 method for class 'FitLandDF' get_comatrix( x, discrete = equal_discrete(2), neighbor = manhattan(1), normalize = normalize_glcm, ... ) ## S3 method for class 'igraph' get_comatrix( x, values, nlevels = length(unique(values)), normalize = normalize_glcm, verbose = TRUE, ... )
get_comatrix(x, ...) ## Default S3 method: get_comatrix(x, ...) ## S3 method for class 'FitLandDF' get_comatrix( x, discrete = equal_discrete(2), neighbor = manhattan(1), normalize = normalize_glcm, ... ) ## S3 method for class 'igraph' get_comatrix( x, values, nlevels = length(unique(values)), normalize = normalize_glcm, verbose = TRUE, ... )
x |
object upon which co-occurrence matrix will be calculated |
... |
additional arguments |
discrete |
function that discretizes object |
neighbor |
function that returns TRUE if two numeric vectors are within
acceptable distance of one another or a single-element |
normalize |
function that normalizes the co-occurrence matrix |
values |
named numeric with values corresponding to the nodes in x. |
nlevels |
int number of levels to discretize into |
verbose |
bool |
co-occurrence matrix
# create fitness landscape as instance of FitLandDF object a <- round(runif(64)) a <- array(a, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(a) # calculate co-occurrence matrix using: # Manhattan distance of 1 # discretization into 2 equal-sized buckets # normalization: multiply all elements so that sum of matrix equals unity comat <- get_comatrix(my_landscape, discrete = equal_discrete(2), neighbor = manhattan(1)) # print co-occurrence matrix print(comat)
# create fitness landscape as instance of FitLandDF object a <- round(runif(64)) a <- array(a, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(a) # calculate co-occurrence matrix using: # Manhattan distance of 1 # discretization into 2 equal-sized buckets # normalization: multiply all elements so that sum of matrix equals unity comat <- get_comatrix(my_landscape, discrete = equal_discrete(2), neighbor = manhattan(1)) # print co-occurrence matrix print(comat)
Convenience function to compute all haralick texture features for a given comat
compute_all_metrics(x)
compute_all_metrics(x)
x |
matrix computed glcm |
data.frame
Calculate the contrast feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
contrast(x, ...) ## Default S3 method: contrast(x, ...) ## S3 method for class 'matrix' contrast(x, ...) ## S3 method for class 'FitLandDF' contrast(x, nlevels, ...)
contrast(x, ...) ## Default S3 method: contrast(x, ...) ## S3 method for class 'matrix' contrast(x, ...) ## S3 method for class 'FitLandDF' contrast(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
contrast metric of x
## calculate contrast of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate contrast contrast(n_x) # calculate contrast of fitness landscape, assuming 2 discrete gray levels vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) contrast(my_landscape, nlevels = 2) ## confirm value of contrast for fitness landscape # extract normalized GLCM from fitness landscape contrast(my_glcm) # should match value of above contrast function call
## calculate contrast of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate contrast contrast(n_x) # calculate contrast of fitness landscape, assuming 2 discrete gray levels vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) contrast(my_landscape, nlevels = 2) ## confirm value of contrast for fitness landscape # extract normalized GLCM from fitness landscape contrast(my_glcm) # should match value of above contrast function call
Calculate the correlation feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
correlation(x, ...) ## Default S3 method: correlation(x, ...) ## S3 method for class 'matrix' correlation(x, ...) ## S3 method for class 'FitLandDF' correlation(x, nlevels, ...)
correlation(x, ...) ## Default S3 method: correlation(x, ...) ## S3 method for class 'matrix' correlation(x, ...) ## S3 method for class 'FitLandDF' correlation(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
correlation metric of x
## calculate correlation of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate correlation correlation(n_x) ## calculate autocorrelation of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate correlation of fitness landscape, assuming 2 discrete gray levels correlation(my_landscape, nlevels = 2) ## confirm value of correlation for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate correlation of extracted GLCM correlation(my_glcm) # should match value of above correlation function call
## calculate correlation of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate correlation correlation(n_x) ## calculate autocorrelation of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate correlation of fitness landscape, assuming 2 discrete gray levels correlation(my_landscape, nlevels = 2) ## confirm value of correlation for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate correlation of extracted GLCM correlation(my_glcm) # should match value of above correlation function call
Takes a numeric variable (could be of class numeric
or integer
) and
returns a discretized version, in which each element has been replaced by a
single integer between 1
and nlevels
, inclusive.
discretize(x, ...) ## S3 method for class 'numeric' discretize(x, nlevels, method = "equal", ...) ## S3 method for class 'list' discretize(x, nlevels, ...) ## S3 method for class 'integer' discretize(x, nlevels, ...) ## S3 method for class 'FitLandDF' discretize(x, nlevels, ...)
discretize(x, ...) ## S3 method for class 'numeric' discretize(x, nlevels, method = "equal", ...) ## S3 method for class 'list' discretize(x, nlevels, ...) ## S3 method for class 'integer' discretize(x, nlevels, ...) ## S3 method for class 'FitLandDF' discretize(x, nlevels, ...)
x |
either a vector ( |
... |
potential additional arguments, currently unnecessary |
nlevels |
positive integer indicating number of discrete categories |
method |
method by which to discretize; split into equal sections by
default ( |
discretized form of x
## discretize a numeric vector vec <- 1:10 discretize(vec, nlevels = 5) # discretize into 5 categories discretize(vec, 2) # discretize into 2 categories ## discretize a fitness landscape # create a 3x3x3 fitness landscape with values 1 through 27 fl_data <- array(1:27, dim = rep(3, 3)) my_fl <- fitscape::FitLandDF(fl_data) discretize(my_fl, nlevels = 2) # discretize landscape into 2 categories discretize(my_fl, 5) # discretize landscape into 5 categories
## discretize a numeric vector vec <- 1:10 discretize(vec, nlevels = 5) # discretize into 5 categories discretize(vec, 2) # discretize into 2 categories ## discretize a fitness landscape # create a 3x3x3 fitness landscape with values 1 through 27 fl_data <- array(1:27, dim = rep(3, 3)) my_fl <- fitscape::FitLandDF(fl_data) discretize(my_fl, nlevels = 2) # discretize landscape into 2 categories discretize(my_fl, 5) # discretize landscape into 5 categories
Calculate the energy feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
energy(x, ...) ## Default S3 method: energy(x, ...) ## S3 method for class 'matrix' energy(x, ...) ## S3 method for class 'FitLandDF' energy(x, nlevels, ...)
energy(x, ...) ## Default S3 method: energy(x, ...) ## S3 method for class 'matrix' energy(x, ...) ## S3 method for class 'FitLandDF' energy(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
energy metric of x
## calculate energy of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate energy energy(n_x) ## calculate energy of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate energy of fitness landscape, assuming 2 discrete gray levels energy(my_landscape, nlevels = 2) ## confirm value of energy for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate energy of extracted GLCM energy(my_glcm) # should match value of above energy function call
## calculate energy of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate energy energy(n_x) ## calculate energy of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate energy of fitness landscape, assuming 2 discrete gray levels energy(my_landscape, nlevels = 2) ## confirm value of energy for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate energy of extracted GLCM energy(my_glcm) # should match value of above energy function call
Calculate the entropy feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
entropy(x, ...) ## Default S3 method: entropy(x, ...) ## S3 method for class 'matrix' entropy(x, ...) ## S3 method for class 'FitLandDF' entropy(x, nlevels, ...)
entropy(x, ...) ## Default S3 method: entropy(x, ...) ## S3 method for class 'matrix' entropy(x, ...) ## S3 method for class 'FitLandDF' entropy(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
entropy metric of x
## calculate entropy of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate entropy entropy(n_x) ## calculate entropy of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate entropy of fitness landscape, assuming 2 discrete gray levels entropy(my_landscape, nlevels = 2) ## confirm value of entropy for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate entropy of extracted GLCM entropy(my_glcm) # should match value of above entropy function call
## calculate entropy of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate entropy entropy(n_x) ## calculate entropy of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate entropy of fitness landscape, assuming 2 discrete gray levels entropy(my_landscape, nlevels = 2) ## confirm value of entropy for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate entropy of extracted GLCM entropy(my_glcm) # should match value of above entropy function call
Returns a function that converts a continuous numeric vector into an integer vector with discrete levels.
equal_discrete(nlevels)
equal_discrete(nlevels)
nlevels |
number of levels to split continuous vector into |
function that makes a numeric vector discrete
# test data x <- 1:10 # create and apply function to split x into 2 discrete levels split_2 <- equal_discrete(2) split_2(x) # create and apply function to split x into 5 discrete levels split_5 <- equal_discrete(5) split_5(x)
# test data x <- 1:10 # create and apply function to split x into 2 discrete levels split_2 <- equal_discrete(2) split_2(x) # create and apply function to split x into 5 discrete levels split_5 <- equal_discrete(5) split_5(x)
Returns a function that checks whether the Euclidean distance between two numeric vectors is less than or equal to a given threshold.
euclidean(dist = 1)
euclidean(dist = 1)
dist |
threshold above which the function will return |
function that checks if Euclidean distance between two vectors
exceeds dist
# test data: Euclidean distance equals sqrt(2) ~ 1.414 x <- rep(0, 5) y <- c(0, 1, 0, 0, 1) # should return TRUE when checking Manhattan distance <= 2 dist_2 <- euclidean(2) dist_2(x, y) # should return FALSE when checking Manhattan distance <= 1 dist_1 <- euclidean(1) dist_1(x, y)
# test data: Euclidean distance equals sqrt(2) ~ 1.414 x <- rep(0, 5) y <- c(0, 1, 0, 0, 1) # should return TRUE when checking Manhattan distance <= 2 dist_2 <- euclidean(2) dist_2(x, y) # should return FALSE when checking Manhattan distance <= 1 dist_1 <- euclidean(1) dist_1(x, y)
GLCM Metrics
xplusy_k(glcm, k) glcm_variance(glcm)
xplusy_k(glcm, k) glcm_variance(glcm)
glcm |
gray level co-occurrence matrix |
k |
summation equal to k |
glcm_variance()
: Variance
Calculate the homogeneity feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
homogeneity(x, ...) ## Default S3 method: homogeneity(x, ...) ## S3 method for class 'matrix' homogeneity(x, ...) ## S3 method for class 'FitLandDF' homogeneity(x, nlevels, ...)
homogeneity(x, ...) ## Default S3 method: homogeneity(x, ...) ## S3 method for class 'matrix' homogeneity(x, ...) ## S3 method for class 'FitLandDF' homogeneity(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
homogeneity metric of x
## calculate homogeneity of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate homogeneity homogeneity(n_x) ## calculate homogeneity of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate homogeneity of fitness landscape, assuming 2 discrete gray levels homogeneity(my_landscape, nlevels = 2) ## confirm value of homogeneity for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate homogeneity of extracted GLCM homogeneity(my_glcm) # should match value of above homogeneity function call
## calculate homogeneity of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate homogeneity homogeneity(n_x) ## calculate homogeneity of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate homogeneity of fitness landscape, assuming 2 discrete gray levels homogeneity(my_landscape, nlevels = 2) ## confirm value of homogeneity for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate homogeneity of extracted GLCM homogeneity(my_glcm) # should match value of above homogeneity function call
Calculate the inverse difference feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
inv_diff(x, ...) ## Default S3 method: inv_diff(x, ...) ## S3 method for class 'matrix' inv_diff(x, ...) ## S3 method for class 'FitLandDF' inv_diff(x, nlevels, ...)
inv_diff(x, ...) ## Default S3 method: inv_diff(x, ...) ## S3 method for class 'matrix' inv_diff(x, ...) ## S3 method for class 'FitLandDF' inv_diff(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
inverse difference metric of x
## calculate inverse difference of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate inverse difference inv_diff(n_x) ## calculate inverse difference of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate inverse difference of fitness landscape, assuming 2 discrete gray levels inv_diff(my_landscape, nlevels = 2) ## confirm value of inverse difference for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate inverse difference of extracted GLCM inv_diff(my_glcm) # should match value of above inv_diff function call
## calculate inverse difference of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate inverse difference inv_diff(n_x) ## calculate inverse difference of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate inverse difference of fitness landscape, assuming 2 discrete gray levels inv_diff(my_landscape, nlevels = 2) ## confirm value of inverse difference for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate inverse difference of extracted GLCM inv_diff(my_glcm) # should match value of above inv_diff function call
Kmeans clustering discretization Splitting of a vector of continuous values into k groups function to discretize using kmeans
kmeans_discrete(nlevels)
kmeans_discrete(nlevels)
nlevels |
number of levels to split continuous vector into |
function that makes a numeric vector discrete
Returns a function that checks whether the Manhattan distance between two numeric vectors is less than or equal to a given threshold.
manhattan(dist = 1)
manhattan(dist = 1)
dist |
threshold above which the function will return |
function that checks if Manhattan distance between two vectors
exceeds dist
# test data: Manhattan distance equals 2 x <- rep(0, 5) y <- c(0, 1, 0, 0, 1) # should return TRUE when checking Manhattan distance <= 3 dist_3 <- manhattan(3) dist_3(x, y) # should return FALSE when checking Manhattan distance <= 1 dist_1 <- manhattan(1) dist_1(x, y)
# test data: Manhattan distance equals 2 x <- rep(0, 5) y <- c(0, 1, 0, 0, 1) # should return TRUE when checking Manhattan distance <= 3 dist_3 <- manhattan(3) dist_3(x, y) # should return FALSE when checking Manhattan distance <= 1 dist_1 <- manhattan(1) dist_1(x, y)
Calculate the maximum probability feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
max_prob(x, ...) ## Default S3 method: max_prob(x, ...) ## S3 method for class 'matrix' max_prob(x, ...) ## S3 method for class 'FitLandDF' max_prob(x, nlevels, ...)
max_prob(x, ...) ## Default S3 method: max_prob(x, ...) ## S3 method for class 'matrix' max_prob(x, ...) ## S3 method for class 'FitLandDF' max_prob(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
maximum probability metric of x
## calculate maximum probability of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate maximum probability max_prob(n_x) ## calculate maximum probability of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate maximum probability of fitness landscape, assuming 2 discrete gray levels max_prob(my_landscape, nlevels = 2) ## confirm value of maximum probability for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate maximum probability of extracted GLCM max_prob(my_glcm) # should match value of above max_prob function call
## calculate maximum probability of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate maximum probability max_prob(n_x) ## calculate maximum probability of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate maximum probability of fitness landscape, assuming 2 discrete gray levels max_prob(my_landscape, nlevels = 2) ## confirm value of maximum probability for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate maximum probability of extracted GLCM max_prob(my_glcm) # should match value of above max_prob function call
Function that normalizes a gray-level co-occurrence matrix (GLCM) so that the sum of all the elements equals unity. This has the added benefit of converting the GLCM to a probability distribution.
normalize_glcm(mat)
normalize_glcm(mat)
mat |
gray-level co-occurrence matrix |
normalized GLCM as a numeric matrix
# normalize an arbitrary matrix a <- matrix(1:9, nrow = 3) n_a <- normalize_glcm(a) print(a) print(n_a)
# normalize an arbitrary matrix a <- matrix(1:9, nrow = 3) n_a <- normalize_glcm(a) print(a) print(n_a)
Function to discretize based on quantiles
quantile_discrete(nlevels)
quantile_discrete(nlevels)
nlevels |
number of levels to split continuous vector into |
function that makes a numeric vector discrete
# test data
# test data
Calculate the sum of squares feature or metric for a gray-level co-occurrence matrix. For definition and application, see Lofstedt et al. (2019) doi:10.1371/journal.pone.0212110.
sum_squares(x, ...) ## Default S3 method: sum_squares(x, ...) ## S3 method for class 'matrix' sum_squares(x, ...) ## S3 method for class 'FitLandDF' sum_squares(x, nlevels, ...)
sum_squares(x, ...) ## Default S3 method: sum_squares(x, ...) ## S3 method for class 'matrix' sum_squares(x, ...) ## S3 method for class 'FitLandDF' sum_squares(x, nlevels, ...)
x |
gray-level co-occurrence matrix |
... |
additional parameters |
nlevels |
desired number of discrete gray levels |
sum of squares metric of x
## calculate sum of squares of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate sum of squares sum_squares(n_x) ## calculate sum of squares of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate sum of squares of fitness landscape, assuming 2 discrete gray levels sum_squares(my_landscape, nlevels = 2) ## confirm value of sum of squares for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate sum of squares of extracted GLCM sum_squares(my_glcm) # should match value of above sum_squares function call
## calculate sum of squares of arbitrary GLCM # define arbitrary GLCM x <- matrix(1:16, nrow = 4) # normalize n_x <- normalize_glcm(x) # calculate sum of squares sum_squares(n_x) ## calculate sum of squares of arbitrary fitness landscape # create fitness landscape using FitLandDF object vals <- runif(64) vals <- array(vals, dim = rep(4, 3)) my_landscape <- fitscape::FitLandDF(vals) # calculate sum of squares of fitness landscape, assuming 2 discrete gray levels sum_squares(my_landscape, nlevels = 2) ## confirm value of sum of squares for fitness landscape # extract normalized GLCM from fitness landscape my_glcm <- get_comatrix(my_landscape, discrete = equal_discrete(2)) # calculate sum of squares of extracted GLCM sum_squares(my_glcm) # should match value of above sum_squares function call