|
| 1 | +"""Define graphene model for nzshm_model gmm logic tree classes.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import logging |
| 5 | +from functools import lru_cache |
| 6 | + |
| 7 | +import graphene |
| 8 | +from graphene import relay |
| 9 | + |
| 10 | +from .nshm_model_sources_schema import get_model_by_version |
| 11 | + |
| 12 | +log = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +# TODO: this method belongs on the nzshm-model gmcm class |
| 16 | +@lru_cache |
| 17 | +def get_branch_set(model_version, short_name): |
| 18 | + glt = get_model_by_version(model_version).gmm_logic_tree |
| 19 | + log.debug(f"glt {glt}") |
| 20 | + for bs in glt.branch_sets: |
| 21 | + if bs.short_name == short_name: |
| 22 | + return bs |
| 23 | + assert 0, f"branch set {short_name} was not found" # pragma: no cover |
| 24 | + |
| 25 | + |
| 26 | +# TODO: this method belongs on the nzshm-model gmcm class |
| 27 | +@lru_cache |
| 28 | +def get_logic_tree_branch(model_version, branch_set_short_name, gsim_name, gsim_args): |
| 29 | + log.info( |
| 30 | + f"get_logic_tree_branch: {branch_set_short_name} gsim_name: {gsim_name} gsim_args: {gsim_args}" |
| 31 | + ) |
| 32 | + branch_set = get_branch_set(model_version, branch_set_short_name) |
| 33 | + for ltb in branch_set.branches: |
| 34 | + if (ltb.gsim_name == gsim_name) and (ltb.gsim_args == json.loads(gsim_args)): |
| 35 | + return ltb |
| 36 | + assert ( |
| 37 | + 0 |
| 38 | + ), f"branch with gsim_name: {gsim_name} gsim_args: {gsim_args} was not found" # pragma: no cover |
| 39 | + |
| 40 | + |
| 41 | +class GmmLogicTreeBranch(graphene.ObjectType): |
| 42 | + class Meta: |
| 43 | + interfaces = (relay.Node,) |
| 44 | + |
| 45 | + model_version = graphene.String() |
| 46 | + branch_set_short_name = graphene.String() |
| 47 | + gsim_name = graphene.String() |
| 48 | + gsim_args = graphene.JSONString() |
| 49 | + tectonic_region_type = graphene.String() # should be an enum |
| 50 | + weight = graphene.Float() |
| 51 | + |
| 52 | + def resolve_id(self, info): |
| 53 | + return f"{self.model_version}|{self.branch_set_short_name}|{self.gsim_name}|{json.dumps(self.gsim_args)}" |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def get_node(cls, info, node_id: str): |
| 57 | + model_version, branch_set_short_name, gsim_name, gsim_args = node_id.split("|") |
| 58 | + gltb = get_logic_tree_branch( |
| 59 | + model_version, branch_set_short_name, gsim_name, gsim_args |
| 60 | + ) |
| 61 | + return GmmLogicTreeBranch( |
| 62 | + model_version=model_version, |
| 63 | + branch_set_short_name=branch_set_short_name, |
| 64 | + tectonic_region_type=gltb.tectonic_region_type, |
| 65 | + gsim_name=gltb.gsim_name, |
| 66 | + gsim_args=gltb.gsim_args, |
| 67 | + weight=gltb.weight, |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +class GmmBranchSet(graphene.ObjectType): |
| 72 | + """Ground Motion Model branch sets, |
| 73 | +
|
| 74 | + to ensure that the wieghts of the enclosed branches sum to 1.0 |
| 75 | + """ |
| 76 | + |
| 77 | + class Meta: |
| 78 | + interfaces = (relay.Node,) |
| 79 | + |
| 80 | + model_version = graphene.String() |
| 81 | + short_name = graphene.String() |
| 82 | + long_name = graphene.String() |
| 83 | + tectonic_region_type = graphene.String() |
| 84 | + branches = graphene.List(GmmLogicTreeBranch) |
| 85 | + |
| 86 | + def resolve_id(self, info): |
| 87 | + return f"{self.model_version}:{self.short_name}" |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def get_node(cls, info, node_id: str): |
| 91 | + model_version, short_name = node_id.split(":") |
| 92 | + bs = get_branch_set(model_version, short_name) |
| 93 | + return GmmBranchSet( |
| 94 | + model_version=model_version, |
| 95 | + tectonic_region_type=bs.tectonic_region_type, |
| 96 | + short_name=bs.short_name, |
| 97 | + long_name=bs.long_name, |
| 98 | + ) |
| 99 | + |
| 100 | + @staticmethod |
| 101 | + def resolve_branches(root, info, **kwargs): |
| 102 | + log.info(f"resolve_branches root: {root} kwargs: {kwargs}") |
| 103 | + bs = get_branch_set(root.model_version, root.short_name) |
| 104 | + for ltb in bs.branches: |
| 105 | + log.debug(ltb) |
| 106 | + yield GmmLogicTreeBranch( |
| 107 | + model_version=root.model_version, |
| 108 | + branch_set_short_name=root.short_name, |
| 109 | + tectonic_region_type=ltb.tectonic_region_type, |
| 110 | + weight=ltb.weight, |
| 111 | + gsim_name=ltb.gsim_name, |
| 112 | + gsim_args=ltb.gsim_args, |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +class GroundMotionModelLogicTree(graphene.ObjectType): |
| 117 | + """A custom Node representing the GMM logic tree of a given model.""" |
| 118 | + |
| 119 | + class Meta: |
| 120 | + interfaces = (relay.Node,) |
| 121 | + |
| 122 | + model_version = graphene.String() |
| 123 | + branch_sets = graphene.List(GmmBranchSet) |
| 124 | + |
| 125 | + def resolve_id(self, info): |
| 126 | + return self.model_version |
| 127 | + |
| 128 | + @classmethod |
| 129 | + def get_node(cls, info, model_version: str): |
| 130 | + return GroundMotionModelLogicTree(model_version=model_version) |
| 131 | + |
| 132 | + @staticmethod |
| 133 | + def resolve_branch_sets(root, info, **kwargs): |
| 134 | + log.info(f"resolve_branch_sets root: {root} kwargs: {kwargs}") |
| 135 | + glt = get_model_by_version(root.model_version).gmm_logic_tree |
| 136 | + for bs in glt.branch_sets: |
| 137 | + yield GmmBranchSet( |
| 138 | + model_version=root.model_version, |
| 139 | + short_name=bs.short_name, |
| 140 | + long_name=bs.long_name, |
| 141 | + tectonic_region_type=bs.tectonic_region_type, |
| 142 | + ) |
0 commit comments