Exemple #1
0
def test_outval_dataframe():
    nvals = 3
    dates = pd.date_range(start='2020-01-01', periods=nvals, freq='YS')
    df = pd.DataFrame({'vals1': range(nvals), 'vals2': range(nvals)}, index=dates)
    outval1 = OutVal(name='test1', ncase=1, val=df['vals1'], ismedian=True)
    assert all(outval1.num == [0, 1, 2])

    outval2 = OutVal(name='test2', ncase=1, val=df.index, ismedian=True)
    assert outval2.val[0] == np.datetime64('2020-01-01T00:00:00.000000000')
Exemple #2
0
    def addOutVal(self,
                  name: str,
                  val: Any,
                  split: bool = True,
                  valmap: dict[Any, float] = None) -> None:
        """
        Generate an OutVal and add it to the dict of outvals.

        Parameters
        ----------
        name : str
            The name of the output value.
        val : Any
            The output value.
        split : bool
            Whether to split the value into its components.
        valmap : dict[Any, float], default: None
            A valmap dict mapping nonnumeric values to numbers.
        """
        if name in self.outvals.keys():
            raise ValueError(f"'{name}' is already an OutVal")

        self.outvals[name] = OutVal(name=name,
                                    ncase=self.ncase,
                                    val=val,
                                    valmap=valmap,
                                    ismedian=self.ismedian)
        if split:
            self.outvals.update(self.outvals[name].split())
Exemple #3
0
    def getMedianVal(self) -> OutVal:
        """
        Get the median value for this output variable if `firstcaseismedian`.

        Returns
        -------
        val : monaco.mc_val.OutVal
            The median output value.
        """
        val = None
        if self.firstcaseismedian:
            val = OutVal(name=self.name, ncase=0, val=self.vals[0],
                         valmap=self.valmap, ismedian=True)
        return val
Exemple #4
0
    def getVal(self, ncase : int) -> OutVal:
        """
        Get the variable value for a specific case.

        Parameters
        ----------
        ncase : int
            The number of the case to get the value for.

        Returns
        -------
        val : monaco.mc_val.OutVal
            The output value.
        """
        ismedian = False
        if (ncase == 0) and self.firstcaseismedian:
            ismedian = True

        val = OutVal(name=self.name, ncase=ncase, val=self.vals[ncase],
                     valmap=self.valmap, ismedian=ismedian)
        return val
Exemple #5
0
def test_outval_valmap_bool_default():
    outval = OutVal(name='test', ncase=1, val=[True, False])
    assert all(outval.num == [1, 0])
Exemple #6
0
def test_outval_valmap_bool():
    outval = OutVal(name='test', ncase=1, val=[True, False], valmap={True: 2, False: 1})
    assert all(outval.num == [2, 1])
Exemple #7
0
def test_outval_valmap_str():
    outval = OutVal(name='test', ncase=1, val=[['a', 'a'], ['b', 'b'], ['a', 'a']], ismedian=True)
    assert outval.valmap == {'a': 0, 'b': 1}
    assert np.array_equal(outval.num, [[0, 0], [1, 1], [0, 0]])
Exemple #8
0
def outval_3_by_2():
    return OutVal(name='test', ncase=1, val=[[0, 0], [0, 0], [0, 0]], ismedian=True)