From b3b0ef73f625c719d3a64e4c985a9108b97a2ccb Mon Sep 17 00:00:00 2001 From: Wenrui Jiang Date: Tue, 23 May 2023 12:26:58 -0400 Subject: [PATCH] remove all the print --- seaduck/get_masks.py | 13 +++++------ seaduck/kernel_weight.py | 16 +++++--------- seaduck/lagrangian.py | 48 ++++++++++++++++++---------------------- seaduck/ocedata.py | 18 +++++++++------ seaduck/runtime_conf.py | 7 +++--- seaduck/smart_read.py | 5 ++--- seaduck/topology.py | 7 +++--- 7 files changed, 53 insertions(+), 61 deletions(-) diff --git a/seaduck/get_masks.py b/seaduck/get_masks.py index 04b5e63b..50d75aba 100644 --- a/seaduck/get_masks.py +++ b/seaduck/get_masks.py @@ -1,4 +1,5 @@ import copy +import logging import warnings import numpy as np @@ -129,7 +130,7 @@ def get_masks(od, tp): keys = od._ds.keys() if "maskC" not in keys: warnings.warn("no maskC in the dataset, assuming nothing is masked.") - print("no maskC in the dataset, assuming nothing is masked.") + logging.warning("no maskC in the dataset, assuming nothing is masked.") # od._ds.C_GRID_VARIABLE.to_masked_array().mask maskC = np.ones_like(od._ds.XC + od._ds.Z) # it is inappropriate to fill in the dataset, @@ -143,14 +144,14 @@ def get_masks(od, tp): "or you could set knw.ignore_mask = True" ) if "maskU" not in keys: - print("creating maskU,this is going to be very slow!") + logging.info("creating maskU,this is going to be very slow!") maskU = mask_u_node(maskC, tp) od._ds["maskU"] = od._ds["Z"] + od._ds["XG"] od._ds["maskU"].values = maskU else: maskU = np.array(od._ds["maskU"]) if "maskV" not in keys: - print("creating maskV,this is going to be very slow!") + logging.info("creating maskV,this is going to be very slow!") maskV = mask_v_node(maskC, tp) od._ds["maskV"] = od._ds["Z"] + od._ds["YG"] od._ds["maskV"].values = maskV @@ -158,7 +159,7 @@ def get_masks(od, tp): maskV = np.array(od._ds["maskV"]) if "maskWvel" not in keys: # there is a maskW with W meaning West in ECCO - print("creating maskW,this is going to be somewhat slow") + logging.info("creating maskW,this is going to be somewhat slow") maskW = mask_w_node(maskC) od._ds["maskWvel"] = od._ds["Z"] + od._ds["YC"] od._ds["maskWvel"].values = maskW @@ -187,8 +188,6 @@ def get_masked(od, ind, gridtype="C"): keys = od._ds.keys() if "maskC" not in keys: warnings.warn("no maskC in the dataset, assuming nothing is masked.") - # print('no maskC in the dataset, assuming nothing is masked.') - # od._ds.C_GRID_VARIABLE.to_masked_array().mask return np.ones_like(ind[0]) elif gridtype == "C": return smart_read(od._ds.maskC, ind) @@ -206,12 +205,10 @@ def get_masked(od, ind, gridtype="C"): small_mask = func_dic[gridtype](maskC, tp) dims = tuple(map(rename_dic[gridtype], od._ds.maskC.dims)) sizes = tuple([len(od._ds[dim]) for dim in dims]) - # print(sizes) mask = np.zeros(sizes) # indexing sensitive old_size = small_mask.shape slices = tuple([slice(0, i) for i in old_size]) - # print(ind_str) mask[slices] = small_mask od._ds[name] = xr.DataArray(mask, dims=dims) return mask[ind] diff --git a/seaduck/kernel_weight.py b/seaduck/kernel_weight.py index ba84aa1f..430b6ac8 100644 --- a/seaduck/kernel_weight.py +++ b/seaduck/kernel_weight.py @@ -1,8 +1,9 @@ import copy +import logging import numpy as np -from seaduck.runtime_conf import compileable, rcParam +from seaduck.runtime_conf import compileable from seaduck.utils import get_combination # default kernel for interpolation. @@ -612,8 +613,7 @@ def get_func(kernel, hkernel="interp", h_order=0): layer_3 = layer_2.get(h_order) if layer_3 is None: - if rcParam["debug_level"] == "very_high": - print("Creating new weight function," " the first time is going to be slow") + logging.info("Creating new weight function, the first time is going to be slow") layer_2[h_order] = kernel_weight(kernel, ktype=hkernel, order=h_order) layer_3 = layer_2[h_order] @@ -686,13 +686,9 @@ def __init__( # Avoid points having same distance ksort_inv = ksort.argsort() - if ( - (inheritance is not None) - and (ignore_mask) - and (rcParam["debug_level"] == "very_high") - ): - print( - "Warning:overwriting the inheritance object to None," + if (inheritance is not None) and (ignore_mask): + logging.info( + "Overwriting the inheritance object to None," " because we ignore masking" ) diff --git a/seaduck/lagrangian.py b/seaduck/lagrangian.py index 1e24fd8d..984b36cf 100644 --- a/seaduck/lagrangian.py +++ b/seaduck/lagrangian.py @@ -1,4 +1,6 @@ import copy +import logging +import warnings import numpy as np @@ -528,7 +530,7 @@ def _out_of_bound(self): # pragma: no cover z_out = False return np.logical_or(np.logical_or(x_out, y_out), z_out) - def trim(self, verbose=False, tol=1e-6): + def trim(self, tol=1e-6): """Move the particles from outside the cell into the cell. At the same time change the velocity accordingly. @@ -536,65 +538,61 @@ def trim(self, verbose=False, tol=1e-6): **Parameters:** - + verbose: Boolean - Whether to dump the maximum value trimmed. - Only set True if something is not going correctly + tol: float The relative tolerance when particles is significantly close to the cell. """ # tol = 1e-6 # about 10 m horizontal for 1 degree - if verbose: # pragma: no cover + if logging.DEBUG >= logging.root.level: # pragma: no cover xmax = np.nanmax(self.rx) xmin = np.nanmin(self.rx) ymax = np.nanmax(self.ry) ymin = np.nanmin(self.ry) + logging.debug(f"converting {xmax} to 0.5") + logging.debug(f"converting {xmin} to -0.5") + logging.debug(f"converting {ymax} to 0.5") + logging.debug(f"converting {ymin} to -0.5") + + if self.rzl_lin is not None: + zmax = np.nanmax(self.rzl_lin) + zmin = np.nanmin(self.rzl_lin) + logging.debug(f"converting {zmax} to 1") + logging.debug(f"converting {zmin} to 0") + # if xmax>=0.5-tol: where = self.rx >= 0.5 - tol cdx = (0.5 - tol) - self.rx[where] self.rx[where] += cdx self.u[where] += self.du[where] * cdx - if verbose: # pragma: no cover - print(f"converting {xmax} to 0.5") # if xmin<=-0.5+tol: where = self.rx <= -0.5 + tol cdx = (-0.5 + tol) - self.rx[where] self.rx[where] += cdx self.u[where] += self.du[where] * cdx - if verbose: # pragma: no cover - print(f"converting {xmin} to -0.5") # if ymax>=0.5-tol: where = self.ry >= 0.5 - tol cdx = (0.5 - tol) - self.ry[where] self.ry[where] += cdx self.v[where] += self.dv[where] * cdx - if verbose: # pragma: no cover - print(f"converting {ymax} to 0.5") # if ymin<=-0.5+tol: where = self.ry <= -0.5 + tol cdx = (-0.5 + tol) - self.ry[where] self.ry[where] += cdx self.v[where] += self.dv[where] * cdx - if verbose: # pragma: no cover - print(f"converting {ymin} to -0.5") if self.rzl_lin is not None: - zmax = np.nanmax(self.rzl_lin) - zmin = np.nanmin(self.rzl_lin) + np.nanmax(self.rzl_lin) + np.nanmin(self.rzl_lin) # if zmax>=1.-tol: where = self.rzl_lin >= 1.0 - tol cdx = (1.0 - tol) - self.rzl_lin[where] self.rzl_lin[where] += cdx self.w[where] += self.dw[where] * cdx - if verbose: # pragma: no cover - print(f"converting {zmax} to 1") # if zmin<=-0.+tol: where = self.rzl_lin <= -0.0 + tol cdx = (-0.0 + tol) - self.rzl_lin[where] self.rzl_lin[where] += cdx self.w[where] += self.dw[where] * cdx - if verbose: # pragma: no cover - print(f"converting {zmin} to 0") def _contract(self): # pragma: no cover """Warp time to move particle into cell. @@ -931,7 +929,7 @@ def to_next_stop(self, t1): elif i > 10: trim_tol = 1e-10 self.trim(tol=trim_tol) - print(sum(todo), "left", " ", end="\r") + logging.debug(sum(todo), "left") self.analytical_step(tf, todo) self.update_after_cell_change() if self.transport: @@ -946,10 +944,9 @@ def to_next_stop(self, t1): if self.save_raw: # record those who cross the wall self.note_taking(todo) - # self._contract() - print(sum(todo), "left", " ", end=" ") - if i == self.max_iteration - 1: - print("maximum iteration count reached") + + if i == self.max_iteration - 1: # pragma: no cover + warnings.warn("maximum iteration count reached") self.t = np.ones(self.N) * t1 if self.ocedata.readiness["time"]: self.it, self.rt, self.dt, self.bt = self.ocedata.find_rel_t(self.t) @@ -1023,7 +1020,7 @@ def to_list_of_time( self.get_u_du() R = [] for i, tl in enumerate(stops): - print(np.datetime64(round(tl), "s")) + logging.info(np.datetime64(round(tl), "s")) if self.save_raw: # save the very start of everything. self.note_taking() @@ -1042,5 +1039,4 @@ def to_list_of_time( R.append(self.deepcopy()) if self.save_raw: self.empty_lists() - print() return stops, R diff --git a/seaduck/ocedata.py b/seaduck/ocedata.py index e6080de2..c2b5f9e2 100644 --- a/seaduck/ocedata.py +++ b/seaduck/ocedata.py @@ -1,3 +1,5 @@ +import logging + import numpy as np try: # pragma: no cover @@ -182,7 +184,9 @@ def hgrid2array(self): """ way = self.readiness["h"] if self.too_large: # pragma: no cover - print("Loading grid into memory, it's a large dataset please be patient") + logging.warning( + "Loading grid into memory, it's a large dataset please be patient" + ) if way == "oceanparcel": for var in ["XC", "YC", "XG", "YG"]: @@ -191,7 +195,7 @@ def hgrid2array(self): try: self[var] = np.array(self[var]).astype("float32") except KeyError: - print(f"no {var} in dataset, skip") + logging.info(f"no {var} in dataset, skip") self[var] = None try: self.dX = np.array(self["dXG"]).astype("float32") @@ -200,10 +204,10 @@ def hgrid2array(self): self.dX = None self.dY = None if self.too_large: # pragma: no cover - print("numpy arrays of grid loaded into memory") + logging.info("numpy arrays of grid loaded into memory") self.tree = create_tree(self.XC, self.YC) if self.too_large: # pragma: no cover - print("cKD created") + logging.info("cKD created") if way == "local_cartesian": for var in ["XC", "YC", "CS", "SN"]: @@ -216,13 +220,13 @@ def hgrid2array(self): try: self[var] = np.array(self[var]).astype("float32") except KeyError: - print(f"no {var} in dataset, skip") + logging.info(f"no {var} in dataset, skip") self[var] = None if self.too_large: # pragma: no cover - print("numpy arrays of grid loaded into memory") + logging.info("numpy arrays of grid loaded into memory") self.tree = create_tree(self.XC, self.YC) if self.too_large: # pragma: no cover - print("cKD created") + logging.info("cKD created") if way == "rectilinear": self.lon = np.array(self["lon"]).astype("float32") diff --git a/seaduck/runtime_conf.py b/seaduck/runtime_conf.py index e9995720..154d215d 100644 --- a/seaduck/runtime_conf.py +++ b/seaduck/runtime_conf.py @@ -1,19 +1,18 @@ rcParam = { "compilable": True, - "debug_level": "not that high", "dump_masks_to_local": False, } try: # pragma: no cover from numba import njit - useJIT = True + rcParam["compilable"] = True except ImportError: # pragma: no cover - useJIT = False + rcParam["compilable"] = False def compileable(func): # pragma: no cover - if useJIT: + if rcParam["compilable"]: return njit(func) else: return func diff --git a/seaduck/smart_read.py b/seaduck/smart_read.py index c3a34092..8a2f3118 100644 --- a/seaduck/smart_read.py +++ b/seaduck/smart_read.py @@ -21,7 +21,6 @@ def smart_read(da, ind, memory_chunk=3, xarray_more_efficient=100): + values: numpy.ndarray The values of the points of interest. Has the same shape as the elements in ind. """ - # print('read called') the_shape = ind[0].shape ind = tuple([i.ravel() for i in ind]) if len(da.dims) != len(ind): @@ -58,7 +57,7 @@ def smart_read(da, ind, memory_chunk=3, xarray_more_efficient=100): ckus, inverse = np.unique(ckbl, axis=0, return_inverse=True) # ckus is the individual chunks used if len(ckus) <= xarray_more_efficient: - # print('use smart') + # logging.debug('use smart') for i, k in enumerate(ckus): ind_str = [] pre = [] @@ -75,6 +74,6 @@ def smart_read(da, ind, memory_chunk=3, xarray_more_efficient=100): result[which] = npck[subind] return result.reshape(the_shape) else: - # print('use xarray') + # logging.debug('use xarray') xrind = tuple([xr.DataArray(dim, dims=["x"]) for dim in ind]) return np.array(da[xrind]).reshape(the_shape) diff --git a/seaduck/topology.py b/seaduck/topology.py index e2e7c04f..ee1637a0 100644 --- a/seaduck/topology.py +++ b/seaduck/topology.py @@ -1,8 +1,9 @@ import copy +import logging import numpy as np -from seaduck.runtime_conf import compileable, rcParam +from seaduck.runtime_conf import compileable # If you have encountered a NotImplementedError and come to this file, # I suggest you read the ***class topology*** near the bottom of this file. @@ -521,8 +522,8 @@ def ind_tend_vec(self, inds, tend, **kwarg): particle_on_edge = True n_ind = ind inds[:, j] = np.array(n_ind).ravel() - if particle_on_edge and rcParam["debug_level"] == "very_high": - print("Warning:Some points are on the edge") + if particle_on_edge: + logging.warning("Some points are on the edge") for i in range(len(inds)): inds[i] = inds[i].astype(int) return inds