<commit_before>"""
Simulation tests.
"""

from carousel.core import (
    logging, data_sources, outputs, formulas, calculations, simulations, models,
    UREG
)
from carousel.contrib.readers import ArgumentReader
from carousel.tests import PROJ_PATH
import numpy as np
import os
import sympy

LOGGER = logging.getLogger(__name__)


def test_make_sim_metaclass():
    """
    Test setting the simulation parameter file as class attributes versus
    specifying the simulation parameter file in the model parameter file.

    :return: simulation
    """

    class SimTest1(simulations.Simulation):
        sim_file = 'Tuscon.json'
        sim_path = os.path.join(PROJ_PATH, 'simulations', 'Standalone')

    sim_test1 = SimTest1()
    return sim_test1


class PythagorasData(data_sources.DataSource):
    data_cache_enabled = False
    data_reader = ArgumentReader
    a = {'units': 'cm', 'argpos': 0}
    b = {'units': 'cm', 'argpos': 2}
    a_unc = {'units': 'cm', 'argpos': 1}
    b_unc = {'units': 'cm', 'argpos': 3}

    def __prepare_data__(self):
        keys = self.parameters.keys()
        for k in keys:
            if k.endswith('_unc'):
                unc = self.data.pop(k)
                self.data_source.pop(k)
                kunc = k[:-4]
                v = self.data[kunc]
                if not unc.dimensionless:
                    unc = (unc / v)
                # raises dimensionality error if not dimensionless
                self.uncertainty[kunc] = {kunc: unc.to(UREG.percent)}
            else:
                self.isconstant[k] = True


class PythagorasOutput(outputs.Output):
    c = {'units': 'cm', 'isconstant': True}


def f_hypotenuse(a, b):
    a, b = np.atleast_1d(a), np.atleast_1d(b)
    return np.sqrt(a * a + b * b).reshape(1, -1)


class PythagorasFormula(formulas.Formula):
    module = 'carousel.tests.test_sim'
    formulas = {
        'f_hypotenuse': {
            'args': ['a', 'b'],
            'units': [('=A', ), ('=A', '=A')],
            'isconstant': []
        }
    }


class PythagorasCalc(calculations.Calc):
    static = [{
        'formula': 'f_hypotenuse',
        'args': {'data': {'a': 'a', 'b': 'b'}},<commit_msg>update test_sim with Parameters<commit_after>"""
Simulation tests.
"""

from carousel.core import (
    logging, data_sources, outputs, formulas, calculations, simulations, models,
    UREG
)
from carousel.core.data_sources import DataParameter
from carousel.core.formulas import FormulaParameter
from carousel.contrib.readers import ArgumentReader
from carousel.tests import PROJ_PATH
import numpy as np
import os
import sympy

LOGGER = logging.getLogger(__name__)


def test_make_sim_metaclass():
    """
    Test setting the simulation parameter file as class attributes versus
    specifying the simulation parameter file in the model parameter file.

    :return: simulation
    """

    class SimTest1(simulations.Simulation):
        sim_file = 'Tuscon.json'
        sim_path = os.path.join(PROJ_PATH, 'simulations', 'Standalone')

    sim_test1 = SimTest1()
    return sim_test1


class PythagorasData(data_sources.DataSource):
    data_cache_enabled = False
    data_reader = ArgumentReader
    a = DataParameter(**{'units': 'cm', 'argpos': 0})
    b = DataParameter(**{'units': 'cm', 'argpos': 2})
    a_unc = DataParameter(**{'units': 'cm', 'argpos': 1})
    b_unc = DataParameter(**{'units': 'cm', 'argpos': 3})

    def __prepare_data__(self):
        keys = self.parameters.keys()
        for k in keys:
            if k.endswith('_unc'):
                unc = self.data.pop(k)
                self.data_source.pop(k)
                kunc = k[:-4]
                v = self.data[kunc]
                if not unc.dimensionless:
                    unc = (unc / v)
                # raises dimensionality error if not dimensionless
                self.uncertainty[kunc] = {kunc: unc.to(UREG.percent)}
            else:
                self.isconstant[k] = True


class PythagorasOutput(outputs.Output):
    c = {'units': 'cm', 'isconstant': True}


def f_hypotenuse(a, b):
    a, b = np.atleast_1d(a), np.atleast_1d(b)
    return np.sqrt(a * a + b * b).reshape(1, -1)


class PythagorasFormula(formulas.Formula):
    f_hypotenuse = FormulaParameter(
        args=['a', 'b'],
        units=[('=A', ), ('=A', '=A')],
        isconstant=[]
    )

    class Meta:
        module = 'carousel.tests.test_sim'


class PythagorasCalc(calculations.Calc):
    static = [{
        'formula': 'f_hypotenuse',
        'args': {'data': {'a': 'a', 'b': 'b'}},
{'fraction_non_alphanumeric': 0.1348908970685475, 'fraction_numerical': 0.0057306590257879654, 'mean_word_length': 2.593032462391132, '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': '892696', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1459, 'n_words': 358}