Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ omit = *tests*

[report]
show_missing = True
exclude_also =
if TYPE_CHECKING:

# Regexes for lines to exclude from consideration
exclude_lines =
Expand Down
46 changes: 39 additions & 7 deletions chainladder/core/common.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
from __future__ import annotations

import numpy as np
import pandas as pd

from chainladder import options
from chainladder.utils.cupy import cp
from chainladder.utils.sparse import sp
from chainladder.utils.dask import dp
import numpy as np
from chainladder.utils.sparse import sp
from chainladder.utils.utility_functions import concat
from chainladder import options

from typing import (
Callable,
Literal,
TYPE_CHECKING
)

if TYPE_CHECKING:
from numpy.typing import ArrayLike
from chainladder.core.typing import TriangleLike



def _get_full_expectation(cdf_, ultimate_, is_cumulative=True):
Expand Down Expand Up @@ -201,15 +215,33 @@ def set_backend(
obj = self.copy()
return obj.set_backend(backend=backend, inplace=True, deep=deep, **kwargs)

def _validate_assumption(self, triangle, value, axis):
@staticmethod
def _validate_assumption(
triangle: TriangleLike,
value: str | int | float | list | tuple | set | np.ndarray | dict | Callable,
axis: Literal[0, 1, 2, 3]
) -> np.ndarray:
"""

Parameters
----------

"""
if type(value) in (int, float, str):
arr = np.repeat(value, triangle.shape[axis])
if type(value) in (list, tuple, set, np.ndarray):
elif type(value) in (list, tuple, set, np.ndarray):
arr = np.array(value)
if type(value) is dict:
elif type(value) is dict:
arr = np.array([value[a] for a in triangle._get_axis_value(axis)])
if callable(value):
elif callable(value):
arr = np.array([value(a) for a in triangle._get_axis_value(axis)])
else:
raise TypeError(
"""
Invalid type provided to value parameter.
Accepted types are str, int, float, list, tuple, set, ndarray, dict, or Callable.
"""
)
if axis == 3:
arr = arr[None, None, None]
if axis == 2:
Expand Down
29 changes: 24 additions & 5 deletions chainladder/core/tests/test_triangle.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from __future__ import annotations

import chainladder as cl
import pandas as pd
import io
import numpy as np
import pandas as pd
import pytest
import io
from datetime import datetime

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from chainladder import Triangle

try:
from IPython.core.display import HTML
except:
except ImportError:
HTML = None


Expand Down Expand Up @@ -1004,4 +1010,17 @@ def test_triangle_init_from_dict() -> None:
cumulative=True
)

assert tri_from_df == tri_from_dict
assert tri_from_df == tri_from_dict


def test_validate_assumption(raa: Triangle) -> None:
"""
Tests Common._validate_assumption.
"""

# Check incorrect type provided to value argument.
with pytest.raises(TypeError):
raa._validate_assumption(
triangle=raa,
value=raa, axis=3 # noqa - incorrect type provided on purpose.
)
Loading
Loading