def __init__(self, loc_cst=False, scale_cst=False, shape_cst=True, **kwargs): ##{{{ l_scale = kwargs.get("l_scale") if l_scale is None: l_scale = sdt.ExpLink() lparams = [] lparams.append({ "name": "loc", "is_cst": loc_cst, "link": kwargs.get("l_loc"), "name_tex": r"\mu" }) lparams.append({ "name": "scale", "is_cst": scale_cst, "link": l_scale, "name_tex": r"\sigma" }) lparams.append({ "name": "shape", "is_cst": shape_cst, "link": kwargs.get("l_shape"), "name_tex": r"\xi" }) AbstractModel.__init__(self, "GEV", sc.genextreme, sd.GEV, lparams, **kwargs)
def default_arg(arg=None): ##{{{ """ Dictionary of arguments of __init__ Parameters ---------- arg : None or dict dictionary of arguments already fixed Returns ------- default: dict Arguments of __init__, elements of "arg" are kept """ default = { "tails": "upper", "link_loc": sdt.IdLink(), "link_scale": sdt.ExpLink(), "link_shape": sdt.LogitLink(-0.5, 0.5), "method": "MLE", "no_test": True, "verbose": False } if arg is not None: for key in arg: default[key] = arg[key] return default
def __init__(self, tails="upper", link_loc=sdt.IdLink(), link_scale=sdt.ExpLink(), link_shape=sdt.LogitLink(-0.5, 0.5), method="MLE", no_test=True, verbose=False): ##{{{ """ """ NSAbstractModel.__init__(self) self._tails = 1 if tails == "upper" else -1 self._gev = sd.GEVLaw(method=method) self._link = { "loc": link_loc, "scale": link_scale, "shape": link_shape } self._no_test = no_test self._verbose = verbose self._loc0 = None self._loc1 = None self._scale0 = None self._scale1 = None self._shape = None self.loc = None self.scale = None self.shape = None self._loct = None self._scalet = None self._shapet = None
def __init__(self, tails="upper", link_scale=sdt.ExpLink(), link_shape=sdt.LogitLink(-0.5, 0.5), method="MLE", verbose=False): ##{{{ """ """ NSAbstractModel.__init__(self) self._tails = 1 if tails == "upper" else -1 self._lf_sc = link_fct_scale self._lf_sh = link_fct_shape self._verbose = verbose self._method = method self._loc0 = None self._loc1 = None self._scale0 = None self._scale1 = None self._shape0 = None self._shape1 = None self.loc = None self.scale = None self.shape = None self._loct = None self._scalet = None self._shapet = None
def __init__(self, loc_cst=False, scale_cst=False, **kwargs): l_scale = kwargs.get("l_scale") if l_scale is None: l_scale = sdt.ExpLink() lparams = [] lparams.append({ "name": "loc", "is_cst": loc_cst, "link": kwargs.get("l_loc"), "name_tex": r"\mu" }) lparams.append({ "name": "scale", "is_cst": scale_cst, "link": l_scale, "name_tex": r"\sigma" }) AbstractModel.__init__(self, "Normal", sc.norm, sd.Normal, lparams, **kwargs)
def __init__(self, link_loc=sdt.IdLink(), link_scale=sdt.ExpLink(), method="MLE", verbose=False): ##{{{ """ Initialization of the NS Gaussian Model """ NSAbstractModel.__init__(self) self._norm = sd.Normal(method=method) self._verbose = verbose self._link = {"loc": link_loc, "scale": link_scale} self.mu0 = None self.mu1 = None self.scale0 = None self.scale1 = None self._mut = None self._scalet = None
def from_netcdf(ifile, ns_law=None): ##{{{ with nc.Dataset(ifile, "r") as ncFile: ## Extract dimensions ##=================== time = np.ma.getdata(ncFile.variables["time"][:]) sample = ncFile.variables["sample"][:] forcing = ncFile.variables["forcing"][:] models = ncFile.variables["models"][:] ns_param = ncFile.variables["ns_param"][:] stats = ncFile.variables["stat"][:] reference = np.ma.getdata(ncFile.variables["reference"][:]) if ns_law is None: ## Extract ns_law attributes ##========================== ns_law_attr = [] for p in ncFile.__dict__: if "ns_law" in p: ns_law_attr.append(p) ## Transform ns_law attributes to ns_law params ##============================================= ns_law_kwargs = {} for p in ns_law_attr: if "ns_law_param" in p: pn, pk = p.split("_")[-2:] if pk == "cst": ns_law_kwargs[pn + "_cst"] = ncFile.__dict__[p] == str(True) if pk == "link": if ncFile.__dict__[p] == str(sdt.ExpLink()): ns_law_kwargs["l_" + pn] = sdt.ExpLink() else: ns_law_kwargs["l_" + pn] = sdt.IdLink() ns_law = None if ncFile.__dict__["ns_law_name"] == "Normal": ns_law = Normal(**ns_law_kwargs) elif ncFile.__dict__["ns_law_name"] == "GEV": ns_law = GEV(**ns_law_kwargs) elif ncFile.__dict__["ns_law_name"] == "GEVMin": ns_law = GEVMin(**ns_law_kwargs) ## Set climatology ##================= clim = Climatology(time, models, ns_law) clim.X = xr.DataArray(np.ma.getdata(ncFile.variables["X"][:]), coords=[time, sample, forcing, models], dims=["time", "sample", "forcing", "models"]) clim.ns_params = xr.DataArray(np.ma.getdata( ncFile.variables["ns_params"][:]), coords=[ns_param, sample, models], dims=["ns_params", "sample", "models"]) clim.stats = xr.DataArray(np.ma.getdata(ncFile.variables["stats"][:]), coords=[time, sample, stats, models], dims=["time", "sample", "stats", "models"]) clim.mm_params.mean = np.ma.getdata(ncFile.variables["mm_mean"][:]) clim.mm_params.cov = np.ma.getdata(ncFile.variables["mm_cov"][:]) ## Set event event = Event(ncFile.event_name, reference.dtype.type(ncFile.event_time), clim.X.dtype.type(ncFile.event_anomaly), reference, ncFile.event_type, ncFile.event_side, ncFile.event_variable, ncFile.event_unit) return clim, event
##===== basepath = os.path.dirname(os.path.abspath(__file__)) pathInp = os.path.join(basepath, "input/Normal") pathOut = os.path.join(basepath, "output/Normal") assert (os.path.exists(pathInp)) assert (os.path.exists(pathOut)) ## Some global parameters ##======================= time_period = np.arange(1850, 2101, 1, dtype=np.int) time_reference = np.arange(1961, 1991, 1, dtype=np.int) n_mcmc_drawn_min = 2500 if is_test else 5000 n_mcmc_drawn_max = 5000 if is_test else 10000 min_rate_accept = 0.05 n_sample = 1000 if not is_test else 10 ns_law = nsm.Normal(l_scale=sdt.ExpLink()) event = ns.Event("HW03", 2003, None, time_reference, name_variable="T", unit_variable="K") verbose = True ci = 0.05 if not is_test else 0.1 ## Load models and observations ##============================= models, lX, lY, Xo, Yo = load_models_obs(pathInp) ## Anomaly from observations ##==========================