Skip to content

Commit

Permalink
Merge pull request #53 from emiliom/folium_gt0
Browse files Browse the repository at this point in the history
Add optional arguments to skip or reduce mesh points map plotting
  • Loading branch information
emiliom authored Dec 15, 2022
2 parents 18a2a58 + 19e6cd9 commit aa0e870
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
27 changes: 21 additions & 6 deletions EchoPro/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def plot_points(gdf: gpd.GeoDataFrame,
return fobj


def plot_layered_points(krig_mesh_obj: KrigingMesh) -> folium.Map:
def plot_layered_points(krig_mesh_obj: KrigingMesh,
plot_mesh_points: bool = True) -> folium.Map:
"""
This function constructs a layered Folium plot.
The layers correspond to the full set of mesh
Expand All @@ -126,6 +127,10 @@ def plot_layered_points(krig_mesh_obj: KrigingMesh) -> folium.Map:
----------
krig_mesh_obj : KrigingMesh
An object specifying the Kriging mesh
plot_mesh_points : bool, default=True
If True, the mesh point layer will be generated.
Set to False to create a lighter-weight Folium plot
that omits the mesh point layer.
Returns
-------
Expand All @@ -144,10 +149,11 @@ def plot_layered_points(krig_mesh_obj: KrigingMesh) -> folium.Map:

fmap = get_folium_map()

# plot mesh points and add them to fmap
folium_layer = folium.FeatureGroup(name='mesh')
folium_layer = plot_points(krig_mesh_obj.mesh_gdf, folium_layer, color='gray')
folium_layer.add_to(fmap)
if plot_mesh_points:
# plot mesh points and add them to fmap
folium_layer = folium.FeatureGroup(name='mesh')
folium_layer = plot_points(krig_mesh_obj.mesh_gdf, folium_layer, color='gray')
folium_layer.add_to(fmap)

# plot the transect points and add them to fmap
folium_layer = folium.FeatureGroup(name='transects')
Expand All @@ -168,7 +174,8 @@ def plot_layered_points(krig_mesh_obj: KrigingMesh) -> folium.Map:

# Visualization function for Kriging
def plot_kriging_results(krig_results_gdf: gpd.GeoDataFrame,
krig_field_name: str) -> folium.Map:
krig_field_name: str,
greater_than_0: bool = False) -> folium.Map:
"""
Constructs a Folium plot depicting the ``krig_field_name``
values at each mesh point.
Expand All @@ -181,6 +188,10 @@ def plot_kriging_results(krig_results_gdf: gpd.GeoDataFrame,
krig_field_name: str
The name of the column in ``krig_results_gdf`` containing
the Kriging values to plot at each mesh point
greater_than_0: bool, default=False
If False, plot kriged values over all points in the mesh.
Set to True to create a lighter-weight Folium plot
that only shows kriged values greater than 0.
Returns
-------
Expand All @@ -192,6 +203,10 @@ def plot_kriging_results(krig_results_gdf: gpd.GeoDataFrame,
# create folium map
fmap = folium.Map(location=[44.61, -125.66], zoom_start=4)

if greater_than_0:
# filter to only values > 0
krig_results_gdf = krig_results_gdf[krig_results_gdf[krig_field_name] > 0]

# collect the appropriate data from the input Dataframe
x_mesh = krig_results_gdf.geometry.x.values
y_mesh = krig_results_gdf.geometry.y.values
Expand Down
65 changes: 47 additions & 18 deletions example_notebooks/echopro_workflow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
"metadata": {},
"outputs": [],
"source": [
"# libraries used in the Notebook\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Python version of EchoPro\n",
"# Python EchoPro\n",
"import EchoPro\n",
"\n",
"# Allows us to grab the SemiVariogram class so we can use its models\n",
Expand All @@ -37,7 +36,8 @@
"\n",
"# Allows us to easily use matplotlib widgets in our Notebook\n",
"%matplotlib widget\n",
"# %matplotlib inline"
"\n",
"EchoPro.__version__"
]
},
{
Expand Down Expand Up @@ -199,12 +199,12 @@
"id": "b8b3fe9b",
"metadata": {},
"source": [
"### Plot the Mesh, Transects and smoothed isobath contour\n",
"### Plot the transects and smoothed isobath contour, and optionally the mesh points\n",
"\n",
"* Generate interactive map using the Folium package\n",
"* Mesh points are in gray\n",
"* `plot_layered_points` generates an interactive map using the Folium package\n",
"* Transect points are represented by a changing color gradient\n",
"* Smoothed contour points (200m isobath) are in blue "
"* Smoothed contour points (200m isobath) are in blue \n",
"* Here we choose not to plot the mesh points to prevent this notebook from getting too big. To plot the mesh points, omit the `plot_mesh_points` argument or use `plot_mesh_points=True`. Mesh points will be in gray"
]
},
{
Expand All @@ -214,7 +214,16 @@
"metadata": {},
"outputs": [],
"source": [
"fmap = plot_layered_points(krig_mesh)\n",
"fmap = plot_layered_points(krig_mesh, plot_mesh_points=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69bd5f94",
"metadata": {},
"outputs": [],
"source": [
"fmap"
]
},
Expand Down Expand Up @@ -413,7 +422,9 @@
"metadata": {},
"source": [
"## Perform Kriging\n",
"* Also generates total biomass at mesh points"
"\n",
"* Generates total biomass at mesh points\n",
"* Computes the total biomass estimate of Kriging output in kmt, where kmt is $10^3$ metric tons, which is equivalent to $10^6$ kg"
]
},
{
Expand All @@ -437,22 +448,42 @@
"krig_results = survey_2019.bio_calc.krig_results_gdf"
]
},
{
"cell_type": "markdown",
"id": "f26b6766-e2fe-4734-8441-f1b8bf4a57ab",
"metadata": {},
"source": [
"Convert from kg to kmt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96b8fd9b-054b-4aa6-a917-c9888a2b5129",
"metadata": {},
"outputs": [],
"source": [
"krig_results.krig_biomass_vals = 1e-6 * krig_results.krig_biomass_vals"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ae97a73c-81b4-4615-babd-b32eb098ee33",
"metadata": {},
"outputs": [],
"source": [
"print(f\"Total Kriged Biomass Estimate: {1e-6*krig_results.krig_biomass_vals.sum():.3f} kmt\")"
"print(f\"Total Kriged Biomass Estimate: {krig_results.krig_biomass_vals.sum():.3f} kmt\")"
]
},
{
"cell_type": "markdown",
"id": "bce6391a",
"metadata": {},
"source": [
"## Plot Kriged Biomass estimate in kmt"
"## Plot Kriged Biomass estimate in kmt\n",
"\n",
"Here we use the argument `greater_than_0=True` to limit the mesh biomass points that are plotted to ones with biomass value > 0, to prevent this notebook from getting too big. To plot all mesh points, omit the `greater_than_0` argument or use `greater_than_0=False`."
]
},
{
Expand All @@ -462,9 +493,7 @@
"metadata": {},
"outputs": [],
"source": [
"# plot all mesh points\n",
"krig_results.krig_biomass_vals = 1e-6 * krig_results.krig_biomass_vals\n",
"plot_kriging_results(krig_results, krig_field_name=\"krig_biomass_vals\")"
"plot_kriging_results(krig_results, krig_field_name=\"krig_biomass_vals\", greater_than_0=True)"
]
},
{
Expand All @@ -478,9 +507,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "echopro_conda",
"display_name": "Python [conda env:echopro]",
"language": "python",
"name": "echopro_conda"
"name": "conda-env-echopro-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -492,7 +521,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
"version": "3.9.13"
},
"toc": {
"base_numbering": 1,
Expand All @@ -510,7 +539,7 @@
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "165px"
"width": "217.2px"
},
"toc_section_display": true,
"toc_window_display": true
Expand Down
12 changes: 6 additions & 6 deletions example_notebooks/transect_selection_workflow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
"metadata": {},
"outputs": [],
"source": [
"fmap = plot_layered_points(krig_mesh)\n",
"fmap = plot_layered_points(krig_mesh, plot_mesh_points=False)\n",
"fmap"
]
},
Expand Down Expand Up @@ -458,9 +458,9 @@
"metadata": {},
"outputs": [],
"source": [
"# plot all mesh points\n",
"# plot mesh points with biomass values > 0\n",
"krig_results.krig_biomass_vals = 1e-6 * krig_results.krig_biomass_vals\n",
"plot_kriging_results(krig_results, krig_field_name=\"krig_biomass_vals\")"
"plot_kriging_results(krig_results, krig_field_name=\"krig_biomass_vals\", greater_than_0=True)"
]
},
{
Expand All @@ -474,9 +474,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "echopro_conda",
"display_name": "Python [conda env:echopro]",
"language": "python",
"name": "echopro_conda"
"name": "conda-env-echopro-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -488,7 +488,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
"version": "3.9.13"
},
"toc": {
"base_numbering": 1,
Expand Down

0 comments on commit aa0e870

Please sign in to comment.