Skip to content

correlation

correlation

Classes for enforcing correlations between branches in logic trees

Correlation dataclass

A correlation between BranchSets of a logic tree. Correlations are used when buiding combinations of branches from differnt BranchSets.

For example, if a logic tree contains branchets A and B each with branches A1, A2 and B1, B2, respectivly, then a correlation A1, B1 will only allow A1-B1 not A1-B2 as valid composite branches.

The primary_brach will not appear in combination with any branches except those identified in associated_branches for the relevent BranchSets

Parameters:

Name Type Description Default
primary_branch Branch

the branch that MUST be correlated with the associated brances.

Branch()
associated_branches List[Branch]

list of branches that the primary_branch must always be coupled with.

list()
weight Optional[float]

weight used for composite branch formed by correlation. Defaults to weight of primary_branch

None
Source code in nzshm_model/logic_tree/correlation.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@dataclass
class Correlation:
    """
    A correlation between **BranchSet**s of a logic tree. Correlations are used when buiding combinations of branches
    from differnt **BranchSet**s.

    For example, if a logic tree contains branchets A and B each with branches A1, A2 and B1, B2, respectivly, then
    a correlation A1, B1 will only allow A1-B1 not A1-B2 as valid composite branches.

    The primary_brach will not appear in combination with any branches except those identified in associated_branches
    for the relevent **BranchSet**s

    Arguments:
        primary_branch: the branch that **MUST** be correlated with the associated brances.
        associated_branches: list of branches that the primary_branch must always be coupled with.
        weight: weight used for composite branch formed by correlation. Defaults to weight of primary_branch
    """

    primary_branch: Branch = field(default_factory=Branch)
    associated_branches: List[Branch] = field(default_factory=list)
    weight: Optional[float] = None

    def __post_init__(self):
        self.weight = self.primary_branch.weight if not self.weight else self.weight

    @property
    def all_branches(self) -> List[Branch]:
        """
        list of all branches in a correlation; both the primary branch and the assiciated branches

        Returns:
            list of branches in correlation
        """
        return [self.primary_branch] + self.associated_branches

all_branches property

list of all branches in a correlation; both the primary branch and the assiciated branches

Returns:

Type Description
List[Branch]

list of branches in correlation

LogicTreeCorrelations dataclass

Bases: Sequence

All correlations for a logic tree.

Parameters:

Name Type Description Default
correlation_groups List[Correlation]

list of correlations to be applied to the logic tree branch sets.

list()
Source code in nzshm_model/logic_tree/correlation.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@dataclass(frozen=True)
class LogicTreeCorrelations(collections.abc.Sequence):
    """
    All correlations for a logic tree.

    Arguments:
        correlation_groups: list of correlations to be applied to the logic tree branch sets.
    """

    correlation_groups: List[Correlation] = field(default_factory=list)

    def __post_init__(self) -> None:
        _validate_correlations(self)

    def primary_branches(self) -> Generator[Branch, None, None]:
        """
        Yield the primary branches of all correlation_groups

        Returns:
            branches
        """
        for cor in self.correlation_groups:
            yield cor.primary_branch

    @overload
    def __getitem__(self, i: int) -> Correlation:
        ...

    @overload
    def __getitem__(self, i: slice) -> Sequence[Correlation]:
        ...

    def __getitem__(self, i):
        if isinstance(i, slice):
            raise TypeError("LogicTreeCorrelations does not support slicing")
        return self.correlation_groups[i]

    def __len__(self) -> int:
        return len(self.correlation_groups)

primary_branches()

Yield the primary branches of all correlation_groups

Returns:

Type Description
None

branches

Source code in nzshm_model/logic_tree/correlation.py
62
63
64
65
66
67
68
69
70
def primary_branches(self) -> Generator[Branch, None, None]:
    """
    Yield the primary branches of all correlation_groups

    Returns:
        branches
    """
    for cor in self.correlation_groups:
        yield cor.primary_branch