Skip to content

Commit

Permalink
Work on Project
Browse files Browse the repository at this point in the history
  • Loading branch information
aaschwanden committed Jun 8, 2024
1 parent e5b3b8a commit 17176c3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ jobs=4 #number of processes to use
[BASIC]
good-names=nameOfYourProject #names to be considered ok
[pre-commit-hook]
disable=bare-except, import-outside-toplevel, too-many-return-statements, line-too-long, invalid-name, too-many-arguments, dangerous-default-value, too-many-locals, duplicate-code
disable=bare-except, import-outside-toplevel, too-many-return-statements, line-too-long, invalid-name, too-many-arguments, dangerous-default-value, too-many-locals, duplicate-code, too-many-statements
34 changes: 32 additions & 2 deletions glacier_flow_tools/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
)

register_colormaps()
# The standard backend is not thread-safe, but 'agg' works with the dask client.
matplotlib.use("agg")


def extract_profile(
Expand Down Expand Up @@ -131,6 +129,7 @@ def extract_profile(
def plot_profile(
ds: xr.Dataset,
result_dir: Path,
interactive: bool = False,
sigma: float = 1.0,
obs_var: str = "v",
obs_error_var: str = "v_err",
Expand Down Expand Up @@ -160,6 +159,9 @@ def plot_profile(
The profile dataset to be plotted.
result_dir : Path
The directory where the result PDF file will be saved.
interactive : bool
If False (default), use non-interactive matplotlib backend for plotting.
Needed for distributed plottinging.
sigma : float, optional
The sigma value to be used for the plot, which determines the width of the Gaussian kernel, by default 1.0.
obs_var : str, optional
Expand Down Expand Up @@ -188,6 +190,13 @@ def plot_profile(
--------
>>> plot_profile(ds, result_dir, sigma=1.0, obs_var='v', obs_error_var='v_err', sim_var='velsurf_mag', palette='Paired')
"""

if interactive:
# The standard backend is not thread-safe, but 'agg' works with the dask client.
matplotlib.use("agg")
else:
matplotlib.use("module://matplotlib_inline.backend_inline")

fig = ds.profiles.plot(
sigma=sigma,
obs_var=obs_var,
Expand All @@ -212,6 +221,7 @@ def plot_glacier(
surface: xr.DataArray,
overlay: xr.DataArray,
result_dir: Union[str, Path],
interactive: bool = False,
cmap: str = "viridis",
vmin: float = 10,
vmax: float = 1500,
Expand All @@ -235,6 +245,9 @@ def plot_glacier(
The overlay to be added to the plot.
result_dir : Union[str, Path]
The directory where the result PDF file will be saved.
interactive : bool
If False (default), use non-interactive matplotlib backend for plotting.
Needed for distributed plottinging.
cmap : str, optional
The colormap to be used for the plot, by default "viridis".
vmin : float, optional
Expand All @@ -252,6 +265,13 @@ def plot_glacier(
--------
>>> plot_glacier(profile_series, surface, overlay, '/path/to/result_dir')
"""

if interactive:
# The standard backend is not thread-safe, but 'agg' works with the dask client.
matplotlib.use("agg")
else:
matplotlib.use("module://matplotlib_inline.backend_inline")

plt.rcParams["font.size"] = fontsize
geom = getattr(profile_series, "geometry")
geom_centroid = geom.centroid
Expand Down Expand Up @@ -1082,6 +1102,7 @@ def extract_profile(

def plot(
self,
interactive: bool = False,
sigma: float = 1,
title: Union[str, None] = None,
obs_var: str = "v",
Expand All @@ -1108,6 +1129,9 @@ def plot(
Parameters
----------
interactive : bool
If False (default), use non-interactive matplotlib backend for plotting.
Needed for distributed plottinging.
sigma : float, optional
The standard deviation of the observations, by default 1.
title : str or None, optional
Expand Down Expand Up @@ -1144,6 +1168,12 @@ def plot(
>>> plot(sigma=1, title='My Plot', obs_var='v', obs_error_var='v_err', sim_var='velsurf_mag', palette='Paired')
"""

if interactive:
# The standard backend is not thread-safe, but 'agg' works with the dask client.
matplotlib.use("agg")
else:
matplotlib.use("module://matplotlib_inline.backend_inline")

plt.rcParams["font.size"] = fontsize
n_exps = self._obj["exp_id"].size
fig = plt.figure(figsize=figsize)
Expand Down
49 changes: 40 additions & 9 deletions notebooks/Ice Thickness Datasets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,15 @@
"metadata": {},
"outputs": [],
"source": [
"profile_resolution = 500 # m"
"profile_resolution = 1_000 # m"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b80a187-b497-42f1-8021-b50925855801",
"cell_type": "markdown",
"id": "f4fab5e4-ab8e-4da6-9745-656df3bf1719",
"metadata": {},
"outputs": [],
"source": [
"# process_profile will use non-interactive \"Agg\", so we switch back to default\n",
"plt.switch_backend('module://matplotlib_inline.backend_inline')"
"## Load observed and simulated velocities"
]
},
{
Expand All @@ -77,7 +74,24 @@
" engine=\"h5netcdf\",\n",
" decode_times=False,\n",
" parallel=True\n",
" )\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "fee49e19-8306-4bc2-977a-e78ccbbdcc68",
"metadata": {},
"source": [
"## Load profile shapefile"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "691c1d3c-0b0f-4698-944b-51bf068b8756",
"metadata": {},
"outputs": [],
"source": [
"profiles_gp = gp.read_file(\"../glacier_flow_tools/data/greenland-flux-gates-5.gpkg\").rename(columns={\"id\": \"profile_id\", \"name\": \"profile_name\"})\n",
"geom = profiles_gp.segmentize(profile_resolution)\n",
"profiles_gp = gp.GeoDataFrame(profiles_gp, geometry=geom)\n",
Expand All @@ -86,6 +100,14 @@
"profile_gp = [p for _, p in profiles_gp.iterrows()][1]"
]
},
{
"cell_type": "markdown",
"id": "0620246f-9ec8-410d-bc19-71ef224267ab",
"metadata": {},
"source": [
"## Plot map-plane view of Jakobshavn gate"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -113,14 +135,23 @@
" stats_kwargs={\"obs_var\": \"v\", \"sim_var\": \"velsurf_mag\"})"
]
},
{
"cell_type": "markdown",
"id": "d12609f9-76e8-4a23-9c4b-f9965f74e4d0",
"metadata": {},
"source": [
"## Plot profile"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6bec873-8faa-48d2-8293-f56ca303ff7e",
"metadata": {},
"outputs": [],
"source": [
"jak_profile.profiles.plot(obs_error_var=None)"
"jak_profile.profiles.plot(obs_error_var=None, interactive=True, figsize=(8, 8), fontsize=12, \n",
" obs_kwargs={\"lw\": 2, \"color\": \"k\", \"marker\": \"o\", \"ms\": 5}, sim_kwargs={\"lw\": 2, \"marker\": \"o\", \"ms\": 5})"
]
},
{
Expand Down

0 comments on commit 17176c3

Please sign in to comment.