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