Skip to content

NshmModel (class)

NshmModel(version, title, source_logic_tree, gmcm_logic_tree, hazard_config)

Bases: Generic[HazardConfigType]

An NshmModel instance represents a complete National Seismic Hazard Model version.

Parameters:

Name Type Description Default
version str

Describes version of the model being developed / used.

required
tite

A title for the model.

required
source_logic_tree SourceLogicTree

The seismicity rate model (SRM) logic tree.

required
gmcm_logic_tree GMCMLogicTree

The ground motion characterization model (GMCM) logic tree.

required
hazard_config HazardConfig

The hazard engine (calculator) configuration.

required
Source code in nzshm_model/model.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def __init__(
    self,
    version: str,
    title: str,
    source_logic_tree: SourceLogicTree,
    gmcm_logic_tree: GMCMLogicTree,
    hazard_config: HazardConfig,
):
    """
    Arguments:
        version: Describes version of the model being developed / used.
        tite: A title for the model.
        source_logic_tree: The seismicity rate model (SRM) logic tree.
        gmcm_logic_tree: The ground motion characterization model (GMCM) logic tree.
        hazard_config: The hazard engine (calculator) configuration.
    """
    self.version = version
    self.title = title
    self.hazard_config = hazard_config
    self.source_logic_tree = source_logic_tree
    self.gmm_logic_tree = gmcm_logic_tree

from_files(version, title, slt_json, gmm_json, hazard_config_json) classmethod

Create a new NshmModel instance from files.

NB library users will typically never use this, rather they will obtain a model instance using static method: get_model_version.

Source code in nzshm_model/model.py
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
@classmethod
def from_files(
    cls,
    version: str,
    title: str,
    slt_json: Union[str, Path],
    gmm_json: Union[str, Path],
    hazard_config_json: Union[str, Path],
) -> 'NshmModel[HazardConfigType]':
    """
    Create a new NshmModel instance from files.

    NB library users will typically never use this, rather they will obtain a model instance
    using static method: `get_model_version`.
    """

    # backwards compatatilbity for v1 SourceLogicTree
    # v1 is not versioned
    data = NshmModel._slt_data_from_file(slt_json)
    if data.get("logic_tree_version") is None:
        source_logic_tree = NshmModel._source_logic_tree_from_v1_json(slt_json)

    source_logic_tree = SourceLogicTree.from_json(slt_json)
    gmcm_logic_tree = GMCMLogicTree.from_json(gmm_json)
    HazardConfigClass = hazard_config_class_factory.get_hazard_config_class_from_file(hazard_config_json)
    hazard_config = HazardConfigClass.from_json(hazard_config_json)
    return cls(version, title, source_logic_tree, gmcm_logic_tree, hazard_config)

get_model_version(version) classmethod

Retrieve an existing model by its specific version

Examples:

>>> from nzshm_model import NshmModel
>>> model = NshmModel.get_model_version("NSHM_v1.0.4")
>>> print(model.title)
>>>
NSHM version 1.0.4, corrected fault geometry

Parameters:

Name Type Description Default
version str

The unique identifier for the model version.

required

Raises:

Type Description
ValueError

when the version does not exist.

Returns:

Type Description
NshmModel

the model instance.

Source code in nzshm_model/model.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
@classmethod
def get_model_version(cls, version: str) -> 'NshmModel':
    """
    Retrieve an existing model by its specific version

    Examples:
        >>> from nzshm_model import NshmModel
        >>> model = NshmModel.get_model_version("NSHM_v1.0.4")
        >>> print(model.title)
        >>>
        NSHM version 1.0.4, corrected fault geometry

    Parameters:
        version: The unique identifier for the model version.

    Raises:
        ValueError: when the version does not exist.

    Returns:
        the model instance.
    """

    model_args_factory = versions.get(version)
    if not model_args_factory:
        raise ValueError(f"{version} is not a valid model version.")

    model_args = model_args_factory()
    model_args['slt_json'] = SLT_SOURCE_PATH / model_args['slt_json']
    model_args['gmm_json'] = GMM_JSON_SOURCE_PATH / model_args['gmm_json']
    model_args['hazard_config_json'] = HAZARD_CONFIG_PATH / model_args['hazard_config_json']
    return cls.from_files(**model_args)

get_source_branch_sets(short_names=None)

get an iterator for the SourceBranchSets matching the specified branch set(s)

Examples:

>>>  model = get_model_version("NSHM_v1.0.4")
>>>  for branch_set in model.get_source_branch_sets(['CRU', 'PUY']):
        print(branch_set.short_name, len(branch_set.branches))
>>>
CRU 36
PUY 3

Parameters:

Name Type Description Default
short_names Union[List[str], str, None]

list of short_names for branch_set(s) (eg. 'HIK', 'CRU', 'PUY', 'SLAB')

None

Raises:

Type Description
ValueError

when a branch short_name is not found.

Yields:

Type Description
SourceBranchSet

iterator of branch_set objects

Source code in nzshm_model/model.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def get_source_branch_sets(self, short_names: Union[List[str], str, None] = None) -> Iterator['SourceBranchSet']:
    """
    get an iterator for the SourceBranchSets matching the specified branch set(s)

    Examples:
        >>>  model = get_model_version("NSHM_v1.0.4")
        >>>  for branch_set in model.get_source_branch_sets(['CRU', 'PUY']):
                print(branch_set.short_name, len(branch_set.branches))
        >>>
        CRU 36
        PUY 3

    Parameters:
        short_names: list of short_names for branch_set(s) (eg. 'HIK', 'CRU', 'PUY', 'SLAB')

    Raises:
        ValueError: when a branch short_name is not found.

    Yields:
        iterator of branch_set objects
    """
    if isinstance(short_names, str):
        list_short_names: List[str] = [short_names]
    else:
        list_short_names = short_names if short_names is not None else []

    if not list_short_names:  # User passes either an empty list or None
        for branch_set in self.source_logic_tree.branch_sets:
            yield branch_set
    else:
        # user has passes a list of short_names
        # check all the names are valid:
        for short_name in list_short_names:
            try:
                for b in filter(lambda item: item.short_name == short_name, self.source_logic_tree.branch_sets):
                    yield (b)
            except StopIteration:
                raise ValueError("The branch " + short_name + " was not found.")

psha_adapter(provider, **kwargs)

get a PSHA adapter for this instance.

Parameters:

Name Type Description Default
provider Type[ModelPshaAdapterInterface]

the adapter class

required
**kwargs Optional[Dict]

additional arguments required by the provider class

{}

Returns:

Type Description
ModelPshaAdapterInterface

a PSHA Adapter instance

Source code in nzshm_model/model.py
171
172
173
174
175
176
177
178
179
180
181
182
183
def psha_adapter(
    self, provider: Type[ModelPshaAdapterInterface], **kwargs: Optional[Dict]
) -> "ModelPshaAdapterInterface":
    """get a PSHA adapter for this instance.

    Arguments:
        provider: the adapter class
        **kwargs: additional arguments required by the provider class

    Returns:
        a PSHA Adapter instance
    """
    return provider(target=self)