Skip to content

Commit

Permalink
🎨 Datashade points to grids with the correct aspect ratio
Browse files Browse the repository at this point in the history
Putting the datashader functionality into the Region class, so that we can make use of the bounding box information! Takes in a pandas.DataFrame table of x, y, z points, and outputs an xarray.DataArray grid for visualization purposes at the pre-set scale. Do some simple algebra math to set the correct aspect ratio with only plot_width as input. Standardized on the variable names to be ds_* for xarray.Datasets and df_* for pandas.Dataframes. Storing all of the intermediate Zarr and Parquet data files into an ATLXI folder. Will update plots in another commit once I sort out some issues, and maybe start a new file called visualization.py to handle the plotting code.
  • Loading branch information
weiji14 committed Jun 2, 2020
1 parent 942968a commit 1248f02
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 143 deletions.
44 changes: 19 additions & 25 deletions atl11_play.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@
"outputs": [],
"source": [
"# Do the actual computation to find data points within region of interest\n",
"region = regions[\"kamb\"] # Select Kamb Ice Stream region\n",
"ds_subset = region.subset(ds=ds)\n",
"placename: str = \"kamb\" # Select Kamb Ice Stream region\n",
"region: deepicedrain.Region = regions[placename]\n",
"ds_subset: xr.Dataset = region.subset(ds=ds)\n",
"ds_subset = ds_subset.unify_chunks()\n",
"ds_subset = ds_subset.compute()"
]
Expand All @@ -301,12 +302,12 @@
}
],
"source": [
"# Save to NetCDF/Zarr formats for distribution\n",
"ds_subset.to_netcdf(\n",
" path=\"atl11_subset.nc\", engine=\"h5netcdf\",\n",
")\n",
"# Save to Zarr/NetCDF formats for distribution\n",
"ds_subset.to_zarr(\n",
" store=\"atl11_subset.zarr\", mode=\"w\", consolidated=True,\n",
" store=f\"ATLXI/ds_subset_{placename}.zarr\", mode=\"w\", consolidated=True,\n",
")\n",
"ds_subset.to_netcdf(\n",
" path=f\"ATLXI/ds_subset_{placename}.nc\", engine=\"h5netcdf\",\n",
")"
]
},
Expand Down Expand Up @@ -599,7 +600,7 @@
"metadata": {},
"outputs": [],
"source": [
"delta_h = dh.dropna(dim=\"ref_pt\").to_dataset(name=\"delta_height\")\n",
"delta_h: xr.Dataset = dh.dropna(dim=\"ref_pt\").to_dataset(name=\"delta_height\")\n",
"delta_h"
]
},
Expand All @@ -609,8 +610,8 @@
"metadata": {},
"outputs": [],
"source": [
"dhdf = delta_h.to_dataframe()\n",
"dhdf.head()"
"df_dh: pd.DataFrame = delta_h.to_dataframe()\n",
"df_dh.head()"
]
},
{
Expand All @@ -619,9 +620,10 @@
"metadata": {},
"outputs": [],
"source": [
"# dhdf.to_parquet(\"temp_dhdf.parquet\")\n",
"# dhdf = pd.read_parquet(\"temp_dhdf.parquet\")\n",
"# dhdf = dhdf.sample(n=1_000_000)"
"# Save or Load delta height data\n",
"# df_dh.to_parquet(f\"ATLXI/df_dh_{placename}.parquet\")\n",
"# df_dh: pd.DataFrame = pd.read_parquet(f\"ATLXI/df_dh_{placename}.parquet\")\n",
"# df_dh = df_dh.sample(n=1_000_000)"
]
},
{
Expand Down Expand Up @@ -681,15 +683,7 @@
"outputs": [],
"source": [
"# Datashade our height values (vector points) onto a grid (raster image)\n",
"canvas = datashader.Canvas(\n",
" plot_width=900,\n",
" plot_height=900,\n",
" x_range=(region.xmin, region.xmax),\n",
" y_range=(region.ymin, region.ymax),\n",
")\n",
"agg_grid = canvas.points(\n",
" source=dhdf, x=\"x\", y=\"y\", agg=datashader.mean(column=\"delta_height\")\n",
")\n",
"agg_grid: xr.DataArray = region.datashade(df=df_dh, z_dim=\"delta_height\")\n",
"agg_grid"
]
},
Expand Down Expand Up @@ -749,7 +743,7 @@
"fig.colorbar(\n",
" position=\"JCR+e\", frame=[\"af\", 'x+l\"Elevation Change from Cycle 5 to 6\"', \"y+lm\"],\n",
")\n",
"fig.savefig(f\"figures/plot_atl11_{placename}.png\")\n",
"fig.savefig(f\"figures/plot_atl11_dh56_{placename}.png\")\n",
"fig.show(width=600)"
]
},
Expand Down Expand Up @@ -798,7 +792,7 @@
"metadata": {},
"outputs": [],
"source": [
"dhdf.hvplot.points(\n",
"df_dh.hvplot.points(\n",
" # title=\"Elevation Change (metres) from Cycle 5 to 6\",\n",
" x=\"x\",\n",
" y=\"y\",\n",
Expand All @@ -824,7 +818,7 @@
"outputs": [],
"source": [
"points = hv.Points(\n",
" data=dhdf,\n",
" data=df_dh,\n",
" kdims=[\"x\", \"y\"],\n",
" vdims=[\"delta_height\"],\n",
" # datatype=[\"xarray\"],\n",
Expand Down
45 changes: 20 additions & 25 deletions atl11_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
longitude=ds.longitude, latitude=ds.latitude
)


# %%
# Also set x, y as coordinates in xarray.Dataset
ds = ds.set_coords(names=["x", "y"])
Expand Down Expand Up @@ -155,18 +156,19 @@

# %%
# Do the actual computation to find data points within region of interest
region = regions["kamb"] # Select Kamb Ice Stream region
ds_subset = region.subset(ds=ds)
placename: str = "kamb" # Select Kamb Ice Stream region
region: deepicedrain.Region = regions[placename]
ds_subset: xr.Dataset = region.subset(ds=ds)
ds_subset = ds_subset.unify_chunks()
ds_subset = ds_subset.compute()

# %%
# Save to NetCDF/Zarr formats for distribution
ds_subset.to_netcdf(
path="atl11_subset.nc", engine="h5netcdf",
)
# Save to Zarr/NetCDF formats for distribution
ds_subset.to_zarr(
store="atl11_subset.zarr", mode="w", consolidated=True,
store=f"ATLXI/ds_subset_{placename}.zarr", mode="w", consolidated=True,
)
ds_subset.to_netcdf(
path=f"ATLXI/ds_subset_{placename}.nc", engine="h5netcdf",
)

# %%
Expand Down Expand Up @@ -279,17 +281,18 @@
dh = dh.persist()

# %%
delta_h = dh.dropna(dim="ref_pt").to_dataset(name="delta_height")
delta_h: xr.Dataset = dh.dropna(dim="ref_pt").to_dataset(name="delta_height")
delta_h

# %%
dhdf = delta_h.to_dataframe()
dhdf.head()
df_dh: pd.DataFrame = delta_h.to_dataframe()
df_dh.head()

# %%
# dhdf.to_parquet("temp_dhdf.parquet")
# dhdf = pd.read_parquet("temp_dhdf.parquet")
# dhdf = dhdf.sample(n=1_000_000)
# Save or Load delta height data
# df_dh.to_parquet(f"ATLXI/df_dh_{placename}.parquet")
# df_dh: pd.DataFrame = pd.read_parquet(f"ATLXI/df_dh_{placename}.parquet")
# df_dh = df_dh.sample(n=1_000_000)

# %% [markdown]
# ## Plot elevation difference for a region
Expand Down Expand Up @@ -326,15 +329,7 @@

# %%
# Datashade our height values (vector points) onto a grid (raster image)
canvas = datashader.Canvas(
plot_width=900,
plot_height=900,
x_range=(region.xmin, region.xmax),
y_range=(region.ymin, region.ymax),
)
agg_grid = canvas.points(
source=dhdf, x="x", y="y", agg=datashader.mean(column="delta_height")
)
agg_grid: xr.DataArray = region.datashade(df=df_dh, z_dim="delta_height")
agg_grid

# %%
Expand Down Expand Up @@ -370,7 +365,7 @@
fig.colorbar(
position="JCR+e", frame=["af", 'x+l"Elevation Change from Cycle 5 to 6"', "y+lm"],
)
fig.savefig(f"figures/plot_atl11_{placename}.png")
fig.savefig(f"figures/plot_atl11_dh56_{placename}.png")
fig.show(width=600)


Expand All @@ -393,7 +388,7 @@
spread_grid

# %%
dhdf.hvplot.points(
df_dh.hvplot.points(
# title="Elevation Change (metres) from Cycle 5 to 6",
x="x",
y="y",
Expand All @@ -413,7 +408,7 @@

# %%
points = hv.Points(
data=dhdf,
data=df_dh,
kdims=["x", "y"],
vdims=["delta_height"],
# datatype=["xarray"],
Expand Down
Loading

0 comments on commit 1248f02

Please sign in to comment.