<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import unittest
import numpy as np

from SimPEG import Mesh
from SimPEG import Maps
from SimPEG import Utils
from SimPEG import Props


class SimpleExample(Props.BaseSimPEG):

    sigmaMap = Props.Mapping(
        "Mapping to the inversion model."
    )

    sigma = Props.PhysicalProperty(
        "Electrical conductivity (S/m)",
        mapping=sigmaMap
    )

    sigmaDeriv = Props.Derivative(
        "Derivative of sigma wrt the model.",
        physical_property=sigma
    )


class ShortcutExample(Props.BaseSimPEG):

    sigma, sigmaMap, sigmaDeriv = Props.Invertible(
        "Electrical conductivity (S/m)"
    )


class ReciprocalMappingExample(Props.BaseSimPEG):

    sigma, sigmaMap, sigmaDeriv = Props.Invertible(
        "Electrical conductivity (S/m)"
    )

    rho, rhoMap, rhoDeriv = Props.Invertible(
        "Electrical resistivity (Ohm m)"
    )

    Props.Reciprocal(sigma, rho)


class ReciprocalExample(Props.BaseSimPEG):

    sigma, sigmaMap, sigmaDeriv = Props.Invertible(
        "Electrical conductivity (S/m)"
    )

    rho = Props.PhysicalProperty(
        "Electrical resistivity (Ohm m)"
    )

    Props.Reciprocal(sigma, rho)


class TestPropMaps(unittest.TestCase):

    def setUp(self):
        pass

    def test_basic(self):
        expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))
        assert expMap.nP == 3

        for Example in [SimpleExample, ShortcutExample]:

            PM = Example(sigmaMap=expMap)
            assert PM.sigmaMap is not None
            assert PM.sigmaMap is expMap

            # There is currently no model, so sigma, which is mapped, fails
            self.assertRaises(AttributeError, getattr, PM, 'sigma')

            PM.model = np.r_[1., 2., 3.]
            assert np.all(PM.sigma == np.exp(np.r_[1., 2., 3.]))
            assert np.all(
                PM.sigmaDeriv.todense() ==
                Utils.sdiag(np.exp(np.r_[1., 2., 3.])).todense()
            )

            # If we set sigma, we should delete the mapping
            PM.sigma = np.r_[1., 2., 3.]
            assert np.all(PM.sigma == np.r_[1., 2., 3.])
            assert PM.sigmaMap is None
            assert PM.sigmaDeriv == 0

            del PM.model
            # sigma is not changed
            assert np.all(PM.sigma == np.r_[1., 2., 3.])

    def test_reciprocal(self):
        expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))

        PM = ReciprocalMappingExample(sigmaMap=expMap)

        PM.model = np.r_[1., 2., 3.]
        assert np.all(PM.sigma == np.exp(np.r_[1., 2., 3.]))
        assert np.all(PM.rho == 1.0 / np.exp(np.r_[1., 2., 3.]))

        PM.rho = np.r_[1., 2., 3.]
        assert PM.rhoMap is None
        assert PM.sigmaMap is None
        assert PM.rhoDeriv == 0
        assert PM.sigmaDeriv == 0
        assert np.all(PM.sigma == 1.0 / np.r_[1., 2., 3.])

        PM.sigmaMap = expMap
        # change your mind?
        PM.rhoMap = expMap
        assert PM._get('sigmaMap') is None
        assert len(PM.rhoMap) == 1
        assert len(PM.sigmaMap) == 2
        assert np.all(PM.rho == np.exp(np.r_[1., 2., 3.]))
        assert np.all(PM.sigma == 1.0 / np.exp(np.r_[1., 2., 3.]))
        assert isinstance(PM.sigmaDeriv.todense(), np.ndarray)

    def test_reciprocal_no_map(self):
        expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))

        PM = ReciprocalExample(sigmaMap=expMap)

        PM.model = np.r_[1., 2., 3.]
        # assert np.all(PM.sigma == np.exp(np.r_[1., 2., 3.]))
        # assert np.all(PM.rho == 1.0 / np.exp(np.r_[1., 2., 3.]))

        # PM.rho = np.r_[1., 2., 3.]
        # assert PM.sigmaMap is None
        # assert PM.sigmaDeriv == 0
        # assert np.all(PM.sigma == 1.0 / np.r_[1., 2., 3.])

        PM.sigmaMap = expMap
        assert len(PM.sigmaMap) == 1
        assert np.all(PM.rho == 1.0 / np.exp(np.r_[1., 2., 3.]))
        assert np.all(PM.sigma == np.exp(np.r_[1., 2., 3.]))
        assert isinstance(PM.sigmaDeriv.todense(), np.ndarray)


if __name__ == '__main__':
    unittest.main()<commit_msg>Uncomment some tests!<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import unittest
import numpy as np

from SimPEG import Mesh
from SimPEG import Maps
from SimPEG import Utils
from SimPEG import Props


class SimpleExample(Props.BaseSimPEG):

    sigmaMap = Props.Mapping(
        "Mapping to the inversion model."
    )

    sigma = Props.PhysicalProperty(
        "Electrical conductivity (S/m)",
        mapping=sigmaMap
    )

    sigmaDeriv = Props.Derivative(
        "Derivative of sigma wrt the model.",
        physical_property=sigma
    )


class ShortcutExample(Props.BaseSimPEG):

    sigma, sigmaMap, sigmaDeriv = Props.Invertible(
        "Electrical conductivity (S/m)"
    )


class ReciprocalMappingExample(Props.BaseSimPEG):

    sigma, sigmaMap, sigmaDeriv = Props.Invertible(
        "Electrical conductivity (S/m)"
    )

    rho, rhoMap, rhoDeriv = Props.Invertible(
        "Electrical resistivity (Ohm m)"
    )

    Props.Reciprocal(sigma, rho)


class ReciprocalExample(Props.BaseSimPEG):

    sigma, sigmaMap, sigmaDeriv = Props.Invertible(
        "Electrical conductivity (S/m)"
    )

    rho = Props.PhysicalProperty(
        "Electrical resistivity (Ohm m)"
    )

    Props.Reciprocal(sigma, rho)


class TestPropMaps(unittest.TestCase):

    def setUp(self):
        pass

    def test_basic(self):
        expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))
        assert expMap.nP == 3

        for Example in [SimpleExample, ShortcutExample]:

            PM = Example(sigmaMap=expMap)
            assert PM.sigmaMap is not None
            assert PM.sigmaMap is expMap

            # There is currently no model, so sigma, which is mapped, fails
            self.assertRaises(AttributeError, getattr, PM, 'sigma')

            PM.model = np.r_[1., 2., 3.]
            assert np.all(PM.sigma == np.exp(np.r_[1., 2., 3.]))
            assert np.all(
                PM.sigmaDeriv.todense() ==
                Utils.sdiag(np.exp(np.r_[1., 2., 3.])).todense()
            )

            # If we set sigma, we should delete the mapping
            PM.sigma = np.r_[1., 2., 3.]
            assert np.all(PM.sigma == np.r_[1., 2., 3.])
            assert PM.sigmaMap is None
            assert PM.sigmaDeriv == 0

            del PM.model
            # sigma is not changed
            assert np.all(PM.sigma == np.r_[1., 2., 3.])

    def test_reciprocal(self):
        expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))

        PM = ReciprocalMappingExample(sigmaMap=expMap)

        PM.model = np.r_[1., 2., 3.]
        assert np.all(PM.sigma == np.exp(np.r_[1., 2., 3.]))
        assert np.all(PM.rho == 1.0 / np.exp(np.r_[1., 2., 3.]))

        PM.rho = np.r_[1., 2., 3.]
        assert PM.rhoMap is None
        assert PM.sigmaMap is None
        assert PM.rhoDeriv == 0
        assert PM.sigmaDeriv == 0
        assert np.all(PM.sigma == 1.0 / np.r_[1., 2., 3.])

        PM.sigmaMap = expMap
        # change your mind?
        PM.rhoMap = expMap
        assert PM._get('sigmaMap') is None
        assert len(PM.rhoMap) == 1
        assert len(PM.sigmaMap) == 2
        assert np.all(PM.rho == np.exp(np.r_[1., 2., 3.]))
        assert np.all(PM.sigma == 1.0 / np.exp(np.r_[1., 2., 3.]))
        assert isinstance(PM.sigmaDeriv.todense(), np.ndarray)

    def test_reciprocal_no_map(self):
        expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))

        PM = ReciprocalExample(sigmaMap=expMap)

        PM.model = np.r_[1., 2., 3.]
        assert np.all(PM.sigma == np.exp(np.r_[1., 2., 3.]))
        assert np.all(PM.rho == 1.0 / np.exp(np.r_[1., 2., 3.]))

        PM.rho = np.r_[1., 2., 3.]
        assert PM.sigmaMap is None
        assert PM.sigmaDeriv == 0
        assert np.all(PM.sigma == 1.0 / np.r_[1., 2., 3.])

        PM.sigmaMap = expMap
        assert len(PM.sigmaMap) == 1
        assert np.all(PM.rho == 1.0 / np.exp(np.r_[1., 2., 3.]))
        assert np.all(PM.sigma == np.exp(np.r_[1., 2., 3.]))
        assert isinstance(PM.sigmaDeriv.todense(), np.ndarray)


if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.13585131894484412, 'fraction_numerical': 0.01990407673860911, 'mean_word_length': 2.0812707794606577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6579492', 'n_tokens_mistral': 3033, 'n_tokens_neox': 2744, 'n_words': 677}