Beispiel #1
0
def test_periodic_crosscorrelation():
    """
    test nonperiodic crosscorrelation
    """
    from pymks import DiscreteIndicatorBasis
    from pymks.stats import crosscorrelate

    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_cross = crosscorrelate(X_, periodic_axes=(0, 1))

    X_result = np.array(
        [
            [
                [0.3, 0.3, 0.3, 0.3],
                [0.2, 0.2, 0.15, 0.2],
                [0.1, 0.1, 0.0, 0.1],
                [0.2, 0.2, 0.15, 0.2],
                [0.3, 0.3, 0.3, 0.3],
            ]
        ]
    )

    assert np.allclose(X_result, X_cross[..., 0])
Beispiel #2
0
def test_crosscorrelate_with_specific_correlations():
    from pymks.stats import crosscorrelate
    from pymks import PrimitiveBasis
    X = np.array([[[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 0],
                   [0, 0, 0, 0]]])
    crosscorrelations = [(1, 2)]
    p_basis = PrimitiveBasis(n_states=3)
    X_cross = crosscorrelate(X, p_basis, crosscorrelations=crosscorrelations)
    X_result = np.array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.],
                         [0., 0., 0., 1 / 12.], [0., 0., 0., 0.]])
    assert np.allclose(X_cross[0, ..., 0], 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)
Beispiel #4
0
def test_periodic_crosscorrelation():
    '''
    test periodic crosscorrelation
    '''
    from pymks import DiscreteIndicatorBasis
    from pymks.stats import crosscorrelate
    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_cross = crosscorrelate(X, basis, periodic_axes=(0, 1))

    X_result = np.array([[[0.3, 0.3, 0.3, 0.3], [0.2, 0.2, 0.15, 0.2],
                          [0.1, 0.1, 0., 0.1], [0.2, 0.2, 0.15, 0.2],
                          [0.3, 0.3, 0.3, 0.3]]])

    assert (np.allclose(X_result, X_cross[..., 0]))
    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)
Beispiel #6
0
def test_crosscorrelate_with_specific_correlations():
    from pymks.stats import crosscorrelate
    from pymks import PrimitiveBasis
    X = np.array([[[0, 0, 0, 0],
                   [0, 1, 0, 0],
                   [0, 0, 2, 0],
                   [0, 0, 0, 0],
                   [0, 0, 0, 0]]])
    crosscorrelations = [(1, 2)]
    p_basis = PrimitiveBasis(n_states=3)
    X_cross = crosscorrelate(X, p_basis, crosscorrelations=crosscorrelations)
    X_result = np.array([[0., 0., 0., 0.],
                         [0., 0., 0., 0.],
                         [0., 0., 0., 0.],
                         [0., 0., 0., 1 / 12.],
                         [0., 0., 0., 0.]])
    assert np.allclose(X_cross[0, ..., 0], X_result)
Beispiel #7
0
def test_nonperiodic_crosscorrelation():
    '''
    test nonperiodic crosscorrelation
    '''
    from pymks import DiscreteIndicatorBasis
    from pymks.stats import crosscorrelate
    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_cross = crosscorrelate(X, basis)

    X_result = np.array([[[
        1 / 3.,
        4 / 9.,
        0.5,
        4 / 9.,
    ], [1 / 8., 0.25, 3 / 16., 0.25], [0., 2 / 15., 0., 2 / 15.],
                          [0., 1 / 12., 0, 1 / 12.], [0, 0, 0, 0]]])
    assert (np.allclose(X_result, X_cross[..., 0]))
Beispiel #8
0
def test_nonperiodic_crosscorrelation():
    '''
    test nonperiodic crosscorrelation
    '''
    from pymks import DiscreteIndicatorBasis
    from pymks.stats import crosscorrelate
    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_cross = crosscorrelate(X, basis)

    X_result = np.array([[[1 / 3., 4 / 9., 0.5,  4 / 9., ],
                          [1 / 8., 0.25, 3 / 16., 0.25],
                          [0., 2 / 15.,  0., 2 / 15.],
                          [0., 1 / 12., 0, 1 / 12.],
                          [0,   0,    0,   0]]])
    assert(np.allclose(X_result, X_cross[..., 0]))
Beispiel #9
0
def test_periodic_crosscorrelation():
    '''
    test periodic crosscorrelation
    '''
    from pymks import DiscreteIndicatorBasis
    from pymks.stats import crosscorrelate
    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_cross = crosscorrelate(X, basis, periodic_axes=(0, 1))

    X_result = np.array([[[0.3, 0.3, 0.3,  0.3],
                          [0.2, 0.2, 0.15, 0.2],
                          [0.1, 0.1,  0., 0.1],
                          [0.2, 0.2, 0.15, 0.2],
                          [0.3, 0.3, 0.3,  0.3]]])

    assert(np.allclose(X_result, X_cross[..., 0]))
Beispiel #10
0
def test_nonperiodic_crosscorrelation():
    """
    test nonperiodic crosscorrelation
    """
    from pymks import DiscreteIndicatorBasis
    from pymks.stats import crosscorrelate

    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_cross = crosscorrelate(X_)

    X_result = np.array(
        [
            [
                [1 / 3.0, 4 / 9.0, 0.5, 4 / 9.0],
                [1 / 8.0, 0.25, 3 / 16.0, 0.25],
                [0.0, 2 / 15.0, 0.0, 2 / 15.0],
                [0.0, 1 / 12.0, 0, 1 / 12.0],
                [0, 0, 0, 0],
            ]
        ]
    )
    assert np.allclose(X_result, X_cross[..., 0])
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)

# Crosscorrelation at (0,0) should be zero
print('Center value : ', X_cross[0, centre, centre, 0])