def test_nonperiodic_autocorrelation(): """ test nonperiodic autocorrelation for spatial statistics """ from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_auto = autocorrelate(X_) X_result = np.array( [ [ [0, 0, 0, 0], [1.0 / 8, 1.0 / 12, 3.0 / 16, 1.0 / 12], [0.2, 2.0 / 15, 0.3, 2.0 / 15], [1.0 / 8, 1.0 / 12, 3.0 / 16, 1.0 / 12], [0, 0, 0, 0], ] ] ) assert np.allclose(X_result, X_auto[..., 1])
def test_mixperdic_mask(): from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate from pymks.datasets import make_checkerboard_microstructure X = make_checkerboard_microstructure(1, 3) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) mask = np.ones((X.shape)) mask[0, 0, 0] = 0 X_auto_mixperiodic_mask = autocorrelate(X_, periodic_axes=[0], confidence_index=mask) X_result_0 = np.array([[[1 / 5.0, 1 / 7.0, 2 / 5.0], [0, 0.5, 0], [2 / 5.0, 1 / 7.0, 1 / 5.0]]]) X_result_1 = np.array([[[2 / 5.0, 1 / 7.0, 2 / 5.0], [0, 0.5, 0.0], [2 / 5.0, 1 / 7.0, 2 / 5.0]]]) X_result = np.concatenate((X_result_0[..., None], X_result_1[..., None]), axis=-1) assert np.allclose(X_auto_mixperiodic_mask, np.concatenate(X_result))
def _get_spatial_correlations(self, X): """ Generates spatial correlations for a microstructure X. Args: X: The microstructure, an `(n_samples, N, ...)` shaped array where `n_samples` is the number of samples and `N` is the spatial discretization. Returns: Autocorrelations and crosscorrelations. """ X_ = self.basis.discretize(X) X_auto = autocorrelate(X_) X_cross = crosscorrelate(X_) return np.concatenate((X_auto, X_cross), axis=-1)
def test_nonperiodic_autocorrelation(): ''' test nonperiodic autocorrelation for spatial statistics ''' from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_auto = autocorrelate(X, basis) X_result = np.array([[[0, 0, 0, 0], [1. / 8, 1. / 12, 3. / 16, 1. / 12], [0.2, 2. / 15, 0.3, 2. / 15], [1. / 8, 1. / 12, 3. / 16, 1. / 12], [0, 0, 0, 0]]]) assert (np.allclose(X_result, X_auto[..., 1]))
def test_periodic_autocorrelation(): ''' test periodic autocorrelation for spatial statistics ''' from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_auto = autocorrelate(X, basis, periodic_axes=(0, 1)) X_result = np.array([[[0, 0, 0, 0], [0.1, 0.1, 0.15, 0.1], [0.2, 0.2, 0.3, 0.2], [0.1, 0.1, 0.15, 0.1], [0, 0, 0, 0]]]) assert (np.allclose(X_result, X_auto[..., 1]))
def test_periodic_autocorrelation(): """ test periodic autocorrelation for spatial statistics """ from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_auto = autocorrelate(X_, periodic_axes=(0, 1)) X_result = np.array( [[[0, 0, 0, 0], [0.1, 0.1, 0.15, 0.1], [0.2, 0.2, 0.3, 0.2], [0.1, 0.1, 0.15, 0.1], [0, 0, 0, 0]]] ) assert np.allclose(X_result, X_auto[..., 1])
def test_autocorrelate_with_specific_correlations(): from pymks.stats import autocorrelate from pymks import PrimitiveBasis X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 2, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) autocorrelations = [(0, 0), (2, 2)] p_basis = PrimitiveBasis(n_states=3) X_auto = autocorrelate(X, p_basis, autocorrelations=autocorrelations) X_result_0 = np.array([[2 / 3., 1 / 3., 5 / 12., 4 / 9.], [5 / 8., 5 / 12., 9 / 16., 1 / 2.], [1 / 2., 7 / 15., 13 / 20., 7 / 15.], [3 / 8., 1 / 2., 9 / 16., 5 / 12.], [1 / 6., 4 / 9., 5 / 12., 1 / 3.]]) assert np.allclose(X_auto[0, ..., 0], X_result_0) X_result_1 = np.array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0.05, 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) assert np.allclose(X_auto[0, ..., 1], X_result_1)
def test_nonperiodic_mask(): ''' test uncertainty masks for nonperiodic axes. ''' from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate from pymks.datasets import make_checkerboard_microstructure X = make_checkerboard_microstructure(1, 3) basis = DiscreteIndicatorBasis(n_states=2) mask = np.ones((X.shape)) mask[0, 0, 0] = 0 X_auto_nonperiodic_mask = autocorrelate(X, basis, confidence_index=mask) X_result_0 = np.array([[[1 / 3., 0, 0.5], [0, 0.5, 0.], [0.5, 0, 1 / 3.]]]) X_result_1 = np.array([[[2 / 3., 0, 0.5], [0, 0.5, 0.], [0.5, 0, 2 / 3.]]]) X_result = np.concatenate((X_result_0[..., None], X_result_1[..., None]), axis=-1) assert np.allclose(X_auto_nonperiodic_mask, np.concatenate(X_result))
def test_mixperdic_mask(): from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate from pymks.datasets import make_checkerboard_microstructure X = make_checkerboard_microstructure(1, 3) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) mask = np.ones((X.shape)) mask[0, 0, 0] = 0 X_auto_mixperiodic_mask = autocorrelate(X_, periodic_axes=[0], confidence_index=mask) X_result_0 = np.array([[[1 / 5., 1 / 7., 2 / 5.], [0, 0.5, 0], [2 / 5., 1 / 7., 1 / 5.]]]) X_result_1 = np.array([[[2 / 5., 1 / 7., 2 / 5.], [0, 0.5, 0.], [2 / 5., 1 / 7., 2 / 5.]]]) X_result = np.concatenate((X_result_0[..., None], X_result_1[..., None]), axis=-1) assert np.allclose(X_auto_mixperiodic_mask, np.concatenate(X_result))
def test_nonperiodic_autocorrelation(): ''' test nonperiodic autocorrelation for spatial statistics ''' from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_auto = autocorrelate(X, basis) X_result = np.array([[[0, 0, 0, 0], [1. / 8, 1. / 12, 3. / 16, 1. / 12], [0.2, 2. / 15, 0.3, 2. / 15], [1. / 8, 1. / 12, 3. / 16, 1. / 12], [0, 0, 0, 0]]]) assert(np.allclose(X_result, X_auto[..., 1]))
import warnings warnings.filterwarnings('ignore') from pymks import PrimitiveBasis from pymks.stats import autocorrelate from pymks.tools import draw_autocorrelations # Create list of autocorrelations corresponding to the binary image lists two_point_correlations_precipitate = [] # Two states (black and white); using primitive basis for microstructure function p_basis = PrimitiveBasis(n_states=2, domain=[0, 1]) for i in range(0, 144): two_point_correlations_precipitate.append( autocorrelate(binary_image_list_precipitate[i].reshape((1, 512, 512)), p_basis, periodic_axes=(0, 1), autocorrelations=[(0, 0)])) # assuming both axes are periodic and only calculating the black autocorrelations two_point_correlations_bicontinuous = [] for i in range(0, 48): two_point_correlations_bicontinuous.append( autocorrelate(binary_image_list_bicontinuous[i].reshape((1, 512, 512)), p_basis, periodic_axes=(0, 1), autocorrelations=[(0, 0)])) two_point_correlations_unknown = [] for i in range(0, 52): two_point_correlations_unknown.append( autocorrelate(binary_image_list_unknown[i].reshape((1, 512, 512)),
from pymks.stats import crosscorrelate from pymks.tools import draw_microstructures from pymks.tools import draw_autocorrelations from pymks.tools import draw_crosscorrelations # Make checkerboard microstructure and observe it. X = make_checkerboard_microstructure(square_size=10, n_squares=6) draw_microstructures(X) # Define the basis for 2-point statistics prim_basis = PrimitiveBasis(n_states=2) X_ = prim_basis.discretize(X) # Computing auto-correlatiions of the microstructure function and drawing the same X_auto = autocorrelate(X, basis=PrimitiveBasis(n_states=2), periodic_axes=(0, 1)) correlations = [('white', 'white'), ('black', 'black')] draw_autocorrelations(X_auto[0], autocorrelations=correlations) # Checking the volume fraction of both the phases i.e. (0,0) value of auto-correlations centre = (X_auto.shape[1] + 1) / 2 print('Volume fraction of black phase', X_auto[0, centre, centre, 0]) print('Volume fraction of black phase', X_auto[0, centre, centre, 1]) # Computing the cross correlation of the microstructure function and drawing the same X_cross = crosscorrelate(X, basis=PrimitiveBasis(n_states=2), periodic_axes=(0, 1)) correlations = [('black', 'white')] draw_crosscorrelations(X_cross[0], crosscorrelations=correlations)