|
30 | 30 | "metadata": {}, |
31 | 31 | "outputs": [], |
32 | 32 | "source": [ |
| 33 | + "import contextlib\n", |
| 34 | + "import os\n", |
| 35 | + "\n", |
33 | 36 | "import cmcrameri as cmc # noqa: F401\n", |
34 | 37 | "import intake\n", |
35 | 38 | "import matplotlib.patches as patches\n", |
36 | 39 | "import matplotlib.pyplot as plt\n", |
37 | 40 | "import numpy as np\n", |
38 | 41 | "import seaborn as sns\n", |
39 | 42 | "import snaphu\n", |
40 | | - "import xarray as xr\n", |
41 | | - "from IPython.display import clear_output" |
| 43 | + "import xarray as xr" |
42 | 44 | ] |
43 | 45 | }, |
44 | 46 | { |
|
120 | 122 | "metadata": {}, |
121 | 123 | "outputs": [], |
122 | 124 | "source": [ |
| 125 | + "@contextlib.contextmanager\n", |
| 126 | + "def suppress_output():\n", |
| 127 | + " with open(os.devnull, \"w\") as devnull:\n", |
| 128 | + " old_stdout = os.dup(1)\n", |
| 129 | + " old_stderr = os.dup(2)\n", |
| 130 | + "\n", |
| 131 | + " os.dup2(devnull.fileno(), 1)\n", |
| 132 | + " os.dup2(devnull.fileno(), 2)\n", |
| 133 | + "\n", |
| 134 | + " try:\n", |
| 135 | + " yield\n", |
| 136 | + " finally:\n", |
| 137 | + " os.dup2(old_stdout, 1)\n", |
| 138 | + " os.dup2(old_stderr, 2)\n", |
| 139 | + " os.close(old_stdout)\n", |
| 140 | + " os.close(old_stderr)\n", |
| 141 | + "\n", |
| 142 | + "\n", |
123 | 143 | "def unwrap_array(\n", |
124 | 144 | " data: xr.DataArray,\n", |
125 | 145 | " complex_var: str = \"cmplx\",\n", |
|
176 | 196 | " coherence = np.ones_like(data_arr.real)\n", |
177 | 197 | "\n", |
178 | 198 | " # Unwrap the phase (already in complex form)\n", |
179 | | - " unw, _ = snaphu.unwrap(\n", |
180 | | - " data_arr,\n", |
181 | | - " coherence,\n", |
182 | | - " nlooks=nlooks,\n", |
183 | | - " cost=cost,\n", |
184 | | - " init=init,\n", |
185 | | - " mask=mask,\n", |
186 | | - " **kwargs,\n", |
187 | | - " )\n", |
188 | | - "\n", |
189 | | - " # Clear the output to avoid printing the snaphu output\n", |
190 | | - " clear_output()\n", |
| 199 | + " with suppress_output():\n", |
| 200 | + " unw, _ = snaphu.unwrap(\n", |
| 201 | + " data_arr,\n", |
| 202 | + " coherence,\n", |
| 203 | + " nlooks=nlooks,\n", |
| 204 | + " cost=cost,\n", |
| 205 | + " init=init,\n", |
| 206 | + " mask=mask,\n", |
| 207 | + " **kwargs,\n", |
| 208 | + " )\n", |
191 | 209 | "\n", |
192 | 210 | " # Build xarray DataArray with the unwrapped phase\n", |
193 | 211 | " # unw_da = xr.DataArray(unw, coords=data.coords, dims=data.dims)\n", |
|
0 commit comments