def test_percent_variance_exceeds_limit_failure(): with pytest.raises(RuntimeError): make_microstructure(n_samples=1, size=(7, 7), n_phases=3, volume_fraction=(0.3, 0.3, 0.4), percent_variance=0.5)
def test_mask_two_samples(): from pymks import DiscreteIndicatorBasis from pymks.stats import correlate from pymks.datasets import make_microstructure X = make_microstructure(n_samples=2, n_phases=2, size=(3, 3), grain_size=(2, 2), seed=99) basis = DiscreteIndicatorBasis(n_states=2) mask = np.ones(X.shape) mask[:, 0, 0] = 0. X_corr = correlate(X, basis, confidence_index=mask) X_result = np.array([[[[1 / 3., 1 / 3., 1 / 3.], [1 / 5., 1 / 5., 1 / 5.], [1 / 4., 1 / 4., 0]], [[1 / 5., 1 / 5., 2 / 5.], [1 / 2., 1 / 2., 0], [1 / 5., 1 / 5., 1 / 5.]], [[1 / 4., 1 / 4., 1 / 2.], [1 / 5., 1 / 5., 2 / 5.], [1 / 3., 1 / 3., 0]]], [[[0., 0., 1 / 3.], [2 / 5., 3 / 5., 0.], [0., 0., 1 / 2.]], [[0., 0., 2 / 5.], [3 / 8., 5 / 8., 0], [0., 0., 3 / 5.]], [[0., 0., 1 / 2.], [2 / 5., 3 / 5., 0.], [0., 0., 2 / 3.]]]]) assert np.allclose(X_corr, X_result)
def test_mask_two_samples(): from pymks import DiscreteIndicatorBasis from pymks.stats import correlate from pymks.datasets import make_microstructure X = make_microstructure(n_samples=2, n_phases=2, size=(3, 3), grain_size=(2, 2), seed=99) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) mask = np.ones(X.shape) mask[:, 0, 0] = 0.0 X_corr = correlate(X_, confidence_index=mask) X_result = np.array( [ [ [[1 / 3.0, 1 / 3.0, 1 / 3.0], [1 / 5.0, 1 / 5.0, 1 / 5.0], [1 / 4.0, 1 / 4.0, 0]], [[1 / 5.0, 1 / 5.0, 2 / 5.0], [1 / 2.0, 1 / 2.0, 0], [1 / 5.0, 1 / 5.0, 1 / 5.0]], [[1 / 4.0, 1 / 4.0, 1 / 2.0], [1 / 5.0, 1 / 5.0, 2 / 5.0], [1 / 3.0, 1 / 3.0, 0]], ], [ [[0.0, 0.0, 1 / 3.0], [2 / 5.0, 3 / 5.0, 0.0], [0.0, 0.0, 1 / 2.0]], [[0.0, 0.0, 2 / 5.0], [3 / 8.0, 5 / 8.0, 0], [0.0, 0.0, 3 / 5.0]], [[0.0, 0.0, 1 / 2.0], [2 / 5.0, 3 / 5.0, 0.0], [0.0, 0.0, 2 / 3.0]], ], ] ) print np.round(X_corr, decimals=4) print X_result assert np.allclose(X_corr, X_result)
def test_percent_variance(): X = make_microstructure(n_samples=1, n_phases=3, volume_fraction=(0.3, 0.2, 0.5), percent_variance=.2) assert np.allclose(np.sum(X == 1) / float(X.size), 0.09, atol=1e-2) assert np.allclose(np.sum(X == 2) / float(X.size), 0.57, atol=1e-2)
def test_3D_volume_fraction(): X = make_microstructure(n_samples=1, n_phases=3, size=(21, 21, 21), volume_fraction=(0.3, 0.1, 0.6)) assert np.allclose(np.sum(X == 0) / float(X.size), 0.3, rtol=1e-3) assert np.allclose(np.sum(X == 1) / float(X.size), 0.1, rtol=1e-3)
def test_3D_percent_variance(): X = make_microstructure(n_samples=1, n_phases=3, size=(21, 21, 21), volume_fraction=(0.7, 0.1, 0.2), percent_variance=.2) assert np.allclose(np.sum(X == 1) / float(X.size), 0.05, atol=1e-2) assert np.allclose(np.sum(X == 2) / float(X.size), 0.2, atol=1e-2)
def test_stats_in_parallel(): from pymks.bases import PrimitiveBasis from pymks.stats import correlate from pymks.datasets import make_microstructure p_basis = PrimitiveBasis(5) X = make_microstructure(n_samples=5, n_phases=3) X_corr_actual = correlate(X, p_basis) for i in range(1, 4): X_corr_test = correlate(X, p_basis, n_jobs=i) assert np.allclose(X_corr_actual, X_corr_test)
def test_3D_percent_variance(): volume_fraction = (0.7, 0.1, 0.2) percent_variance = .2 X = make_microstructure(n_samples=1, n_phases=3, size=(21, 21, 21), volume_fraction=volume_fraction, percent_variance=percent_variance) vf_1 = np.sum(X == 1) / float(X.size) vf_2 = np.sum(X == 2) / float(X.size) assert vf_1 > volume_fraction[1] - percent_variance assert vf_1 < volume_fraction[1] + percent_variance assert vf_2 > volume_fraction[2] - percent_variance assert vf_2 < volume_fraction[2] + percent_variance
def test_stats_in_parallel(): import time from pymks.bases import PrimitiveBasis from pymks.stats import correlate from pymks.datasets import make_microstructure p_basis = PrimitiveBasis(5) if p_basis._pyfftw: X = make_microstructure(n_samples=5, n_phases=3) t = [] for i in range(1, 4): t_start = time.time() correlate(X, p_basis, n_jobs=i) t.append(time.time() - t_start) assert t == sorted(t, reverse=True) else: pass
def test_volume_fraction_failure(): make_microstructure(n_samples=1, volume_fraction=(0.3, 0.6))
def test_volume_fraction_with_n_phases_failure(): make_microstructure(n_samples=1, size=(7, 7), n_phases=3, volume_fraction=(0.5, 0.5))
def test_volume_fraction(): X = make_microstructure(n_samples=1, n_phases=3, volume_fraction=(0.3, 0.2, 0.5)) assert np.allclose(np.sum(X == 1) / float(X.size), 0.2, rtol=1e-4) assert np.allclose(np.sum(X == 2) / float(X.size), 0.5, atol=1e-4)
def test_size_and_grain_size_failure(): make_microstructure(n_samples=1, size=(7, 7), grain_size=(8, 1))
def test_volume_fraction_with_n_phases_failure(): with pytest.raises(RuntimeError): make_microstructure(n_samples=1, size=(7, 7), n_phases=3, volume_fraction=(0.5, 0.5))
def test_volume_fraction_failure(): with pytest.raises(RuntimeError): make_microstructure(n_samples=1, volume_fraction=(0.3, 0.6))
from AbaqusIO import generateAbaqusInp from pymks.datasets import make_microstructure X = make_microstructure() generateAbaqusInp('test_microstructure', X[0], elastic_modulus=(120, 80), poissions_ratio=(0.3, 0.3))
def test_size_and_grain_size_failure(): with pytest.raises(RuntimeError): make_microstructure(n_samples=1, size=(7, 7), grain_size=(8, 1))
from pymks.datasets import make_microstructure from AbaqusIO import generateAbaqusInp n_phases = 2 grain_size1 = (20, 2, 2) size = (21, 21, 21) Long_fiber_x = make_microstructure(n_samples=2, size=size, n_phases=n_phases, grain_size=grain_size1, seed=1) Ab_fiber = Long_fiber_x+1 generateAbaqusInp('test_microstructure', Ab_fiber[1], elastic_modulus=(120, 80), poissions_ratio=(0.3, 0.3)) print "Done"