<commit_before>import numpy as np
from pymks import ElasticFESimulation
from pymks import MKSRegressionModel

__all__ = ['make_delta_microstructures', 'make_elasticFEstrain_delta', 'make_elasticFEstrain_random', 'ElasticFESimulation']

def make_elasticFEstrain_delta(elastic_modulus, poissons_ratio, size, strain_index=0):
    """Generate delta microstructures and responses

    Simple interface to generate delta microstructures and their
    strain response fields that can be used for the fit method in the
    `MKSRegressionModel`. The length of `elastic_modulus` and
    `poissons_ratio` indicates the number of phases in the
    microstructure. The following example is or a two phase
    microstructure with dimensions of `(5, 5)`.

    >>> elastic_modulus = (1., 2.)
    >>> poissons_ratio = (0.3, 0.3)
    >>> X, y = make_elasticFEstrain_delta(elastic_modulus=elastic_modulus,
    ...                                   poissons_ratio=poissons_ratio,
    ...                                   size=(5, 5)) #doctest: +ELLIPSIS
    sfepy: ...

    `X` is the delta microstructures, and `y` is the
    strain response fields.

    Args:
      elastic_modulus: list of elastic moduli for the phases
      poissons_ratio: list of Poisson's ratios for the phases
      size: size of the microstructure
      strain_index: interger value to return a particular strain
        field.  0 returns exx, 1 returns eyy, etc. To return all
        strain fields set strain_index equal to slice(None).

    Returns:
      tuple containing delta microstructures and their strain fields

    """
    FEsim = ElasticFESimulation(elastic_modulus=elastic_modulus,
                                poissons_ratio=poissons_ratio)

    X = make_delta_microstructures(len(elastic_modulus), size=size)
    return X, FEsim.get_response(X, strain_index=strain_index)

def make_delta_microstructures(Nphases, size):
    """Constructs delta microstructures

    Constructs delta microstructures for an arbitrary number of phases
    given the size of the domain.

    >>> X = np.array([[[[0, 0, 0],
    ...                 [0, 0, 0],
    ...                 [0, 0, 0]],
    ...                [[0, 0, 0],
    ...                 [0, 1, 0],
    ...                 [0, 0, 0]],
    ...                [[0, 0, 0],
    ...                 [0, 0, 0],
    ...                 [0, 0, 0]]],
    ...               [[[1, 1, 1],
    ...                 [1, 1, 1],
    ...                 [1, 1, 1]],
    ...                [[1, 1, 1],
    ...                 [1, 0, 1],
    ...                 [1, 1, 1]],
    ...                [[1, 1, 1],
    ...                 [1, 1, 1],
    ...                 [1, 1, 1]]]])

    >>> assert(np.allclose(X, make_delta_microstructures(2, size=(3, 3, 3))))

    Args:
        Nphases: number of phases
        size: dimension of microstructure

    Returns:
        delta microstructures for the system of shape
        (Nsamples, Nx, Ny, ...)

    """
    shape = (Nphases, Nphases) + size
    center = tuple((np.array(size) - 1) / 2)
    X = np.zeros(shape=shape, dtype=int)
    X[:] = np.arange(Nphases)[(slice(None), None) + (None,) * len(size)]
    X[(slice(None), slice(None)) + center] = np.arange(Nphases)
    mask = ~np.identity(Nphases, dtype=bool)
    return X[mask]

def make_elasticFEstrain_random(n_samples, elastic_modulus, poissons_ratio,
                                size, strain_index=0):
    """Generate delta microstructures and responses

    Simple interface to generate delta microstructures and their
    strain response fields that can be used for the fit method in the
    `MKSRegressionModel`. The length of `elastic_modulus` and
    `poissons_ratio` indicates the number of phases in the
    microstructure. The following example is or a two phase
    microstructure with dimensions of `(5, 5)`.

    >>> elastic_modulus = (1., 2.)
    >>> poissons_ratio = (0.3, 0.3)
    >>> X, y = make_elasticFEstrain_random(n_samples=1,
    ...                                    elastic_modulus=elastic_modulus,
    ...                                    poissons_ratio=poissons_ratio,
    ...                                    size=(5, 5)) #doctest: +ELLIPSIS
    sfepy: ...

    `X` is the delta microstructures, and `y` is the
    strain response fields.

    Args:
      elastic_modulus: list of elastic moduli for the phases
      poissons_ratio: list of Poisson's ratios for the phases
      n_samples: number of microstructure samples
      size: size of the microstructure
      strain_index: interger value to return a particular strain
        field.  0 returns exx, 1 returns eyy, etc. To return all
        strain fields set strain_index equal to slice(None).

    Returns:
      tuple containing delta microstructures and their strain fields

    """
    FEsim = ElasticFESimulation(elastic_modulus=elastic_modulus,
                                poissons_ratio=poissons_ratio)

    X = np.random.randint(len(elastic_modulus), size=((n_samples,)+size))
    return X, FEsim.get_response(X, strain_index=strain_index)
<commit_msg>Add macroscopic strain to make_elasticFEstrain functions<commit_after>import numpy as np
from pymks import ElasticFESimulation
from pymks import MKSRegressionModel

__all__ = ['make_delta_microstructures', 'make_elasticFEstrain_delta',
           'make_elasticFEstrain_random', 'ElasticFESimulation']

def make_elasticFEstrain_delta(elastic_modulus, poissons_ratio, 
                               size, strain=1.0, strain_index=0):
    """Generate delta microstructures and responses

    Simple interface to generate delta microstructures and their
    strain response fields that can be used for the fit method in the
    `MKSRegressionModel`. The length of `elastic_modulus` and
    `poissons_ratio` indicates the number of phases in the
    microstructure. The following example is or a two phase
    microstructure with dimensions of `(5, 5)`.

    >>> elastic_modulus = (1., 2.)
    >>> poissons_ratio = (0.3, 0.3)
    >>> X, y = make_elasticFEstrain_delta(elastic_modulus=elastic_modulus,
    ...                                   poissons_ratio=poissons_ratio,
    ...                                   size=(5, 5)) #doctest: +ELLIPSIS
    sfepy: ...

    `X` is the delta microstructures, and `y` is the
    strain response fields.

    Args:
      elastic_modulus: list of elastic moduli for the phases
      poissons_ratio: list of Poisson's ratios for the phases
      size: size of the microstructure
      strain_index: interger value to return a particular strain
        field.  0 returns exx, 1 returns eyy, etc. To return all
        strain fields set strain_index equal to slice(None).

    Returns:
      tuple containing delta microstructures and their strain fields

    """
    FEsim = ElasticFESimulation(elastic_modulus=elastic_modulus,
                                poissons_ratio=poissons_ratio,
                                strain=strain)

    X = make_delta_microstructures(len(elastic_modulus), size=size)
    return X, FEsim.get_response(X, strain_index=strain_index)

def make_delta_microstructures(Nphases, size):
    """Constructs delta microstructures

    Constructs delta microstructures for an arbitrary number of phases
    given the size of the domain.

    >>> X = np.array([[[[0, 0, 0],
    ...                 [0, 0, 0],
    ...                 [0, 0, 0]],
    ...                [[0, 0, 0],
    ...                 [0, 1, 0],
    ...                 [0, 0, 0]],
    ...                [[0, 0, 0],
    ...                 [0, 0, 0],
    ...                 [0, 0, 0]]],
    ...               [[[1, 1, 1],
    ...                 [1, 1, 1],
    ...                 [1, 1, 1]],
    ...                [[1, 1, 1],
    ...                 [1, 0, 1],
    ...                 [1, 1, 1]],
    ...                [[1, 1, 1],
    ...                 [1, 1, 1],
    ...                 [1, 1, 1]]]])

    >>> assert(np.allclose(X, make_delta_microstructures(2, size=(3, 3, 3))))

    Args:
        Nphases: number of phases
        size: dimension of microstructure

    Returns:
        delta microstructures for the system of shape
        (Nsamples, Nx, Ny, ...)

    """
    shape = (Nphases, Nphases) + size
    center = tuple((np.array(size) - 1) / 2)
    X = np.zeros(shape=shape, dtype=int)
    X[:] = np.arange(Nphases)[(slice(None), None) + (None,) * len(size)]
    X[(slice(None), slice(None)) + center] = np.arange(Nphases)
    mask = ~np.identity(Nphases, dtype=bool)
    return X[mask]

def make_elasticFEstrain_random(n_samples, elastic_modulus, poissons_ratio,
                                size, strain=1.0, strain_index=0):
    """Generate delta microstructures and responses

    Simple interface to generate delta microstructures and their
    strain response fields that can be used for the fit method in the
    `MKSRegressionModel`. The length of `elastic_modulus` and
    `poissons_ratio` indicates the number of phases in the
    microstructure. The following example is or a two phase
    microstructure with dimensions of `(5, 5)`.

    >>> elastic_modulus = (1., 2.)
    >>> poissons_ratio = (0.3, 0.3)
    >>> X, y = make_elasticFEstrain_random(n_samples=1,
    ...                                    elastic_modulus=elastic_modulus,
    ...                                    poissons_ratio=poissons_ratio,
    ...                                    size=(5, 5)) #doctest: +ELLIPSIS
    sfepy: ...

    `X` is the delta microstructures, and `y` is the
    strain response fields.

    Args:
      elastic_modulus: list of elastic moduli for the phases
      poissons_ratio: list of Poisson's ratios for the phases
      n_samples: number of microstructure samples
      size: size of the microstructure
      strain_index: interger value to return a particular strain
        field.  0 returns exx, 1 returns eyy, etc. To return all
        strain fields set strain_index equal to slice(None).

    Returns:
      tuple containing delta microstructures and their strain fields

    """
    FEsim = ElasticFESimulation(elastic_modulus=elastic_modulus,
                                poissons_ratio=poissons_ratio,
                                strain=strain)

    X = np.random.randint(len(elastic_modulus), size=((n_samples,)+size))
    return X, FEsim.get_response(X, strain_index=strain_index)

{'fraction_non_alphanumeric': 0.10294117647058823, 'fraction_numerical': 0.01722136222910217, 'mean_word_length': 2.0774039892825247, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4120738', 'n_tokens_mistral': 3246, 'n_tokens_neox': 2950, 'n_words': 925}