Skip to content

Commit

Permalink
docs(examples): fix paths
Browse files Browse the repository at this point in the history
  • Loading branch information
nialov committed Jan 9, 2024
1 parent e95f28b commit 09e527a
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 127 deletions.
102 changes: 30 additions & 72 deletions examples/example_networks.py → examples/example_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,100 +25,56 @@
# the user.

from pathlib import Path
from urllib.error import URLError

import geopandas as gpd

from fractopo import Network

# Root fractopo repository directory path
PROJECT_BASE_PATH = Path("..")

# TODO: Clean up
def remote_or_local_read_file(url: str, path: Path):

def read_path_from_base(path: Path, project_base_path: Path = PROJECT_BASE_PATH):
"""
Try to read remotely and fallback to local without internet.
Prefix path with project base path.
"""
try:
return gpd.read_file(url)
except URLError:
try:
return gpd.read_file(Path(__file__).parent.parent / path)
except Exception:
message = f"""
{ Path(__file__) }
{ Path(__file__).parent }
{ Path(__file__).parent.parent }
{ Path(__file__).parent.parent / path }
{ (Path(__file__).parent.parent / path).parent.glob("*") }
"""
raise FileNotFoundError(message)


kb11_trace_gdf = remote_or_local_read_file(
url=(
"https://raw.githubusercontent.com/nialov/"
"fractopo/master/tests/sample_data/KB11/KB11_traces.geojson"
),
return gpd.read_file(project_base_path / path)


KB11_TRACE_GDF = read_path_from_base(
path=Path("tests/sample_data/KB11/KB11_traces.geojson"),
)

kb11_area_gdf = remote_or_local_read_file(
url=(
"https://raw.githubusercontent.com/nialov/"
"fractopo/master/tests/sample_data/KB11/KB11_area.geojson"
),
KB11_AREA_GDF = read_path_from_base(
path=Path("tests/sample_data/KB11/KB11_area.geojson"),
)
kb7_trace_gdf = remote_or_local_read_file(
url=(
"https://raw.githubusercontent.com/nialov/"
"fractopo/master/tests/sample_data/KB7/KB7_traces.geojson"
),
KB7_TRACE_GDF = read_path_from_base(
path=Path("tests/sample_data/KB7/KB7_traces.geojson"),
)
kb7_area_gdf = remote_or_local_read_file(
url=(
"https://raw.githubusercontent.com/nialov/"
"fractopo/master/tests/sample_data/KB7/KB7_area.geojson"
),
KB7_AREA_GDF = read_path_from_base(
path=Path("tests/sample_data/KB7/KB7_area.geojson"),
)

hastholmen_trace_gdf = remote_or_local_read_file(
url=(
"https://raw.githubusercontent.com/nialov/"
"fractopo/master/tests/sample_data/hastholmen_traces_validated.geojson"
),
HASTHOLMEN_TRACE_GDF = read_path_from_base(
path=Path("tests/sample_data/hastholmen_traces_validated.geojson"),
)
hastholmen_area_gdf = remote_or_local_read_file(
url=(
"https://raw.githubusercontent.com/nialov/"
"fractopo/master/tests/sample_data/hastholmen_area.geojson"
),
HASTHOLMEN_AREA_GDF = read_path_from_base(
path=Path("tests/sample_data/hastholmen_area.geojson"),
)

lidar200k_trace_gdf = remote_or_local_read_file(
url=(
"https://raw.githubusercontent.com/nialov/"
"fractopo/master/tests/sample_data/traces_200k.geojson"
),
LIDAR_200K_TRACE_GDF = read_path_from_base(
path=Path("tests/sample_data/traces_200k.geojson"),
)

lidar200k_area_gdf = remote_or_local_read_file(
url=(
"https://raw.githubusercontent.com/nialov/"
"fractopo/master/tests/sample_data/area_200k.geojson"
),
LIDAR_200K_AREA_GDF = read_path_from_base(
path=Path("tests/sample_data/area_200k.geojson"),
)


kb11_network = Network(
KB11_NETWORK = Network(
name="KB11",
trace_gdf=kb11_trace_gdf,
area_gdf=kb11_area_gdf,
trace_gdf=KB11_TRACE_GDF,
area_gdf=KB11_AREA_GDF,
truncate_traces=True,
circular_target_area=False,
determine_branches_nodes=True,
Expand All @@ -128,20 +84,20 @@ def remote_or_local_read_file(url: str, path: Path):
azimuth_set_ranges=((135, 45), (45, 135)),
)

kb7_network = Network(
KB7_NETWORK = Network(
name="KB7",
trace_gdf=kb7_trace_gdf,
area_gdf=kb7_area_gdf,
trace_gdf=KB7_TRACE_GDF,
area_gdf=KB7_AREA_GDF,
truncate_traces=True,
circular_target_area=False,
determine_branches_nodes=True,
snap_threshold=0.001,
)

hastholmen_network = Network(
HASTHOLMEN_NETWORK = Network(
name="Hastholmen",
trace_gdf=hastholmen_trace_gdf,
area_gdf=hastholmen_area_gdf,
trace_gdf=HASTHOLMEN_TRACE_GDF,
area_gdf=HASTHOLMEN_AREA_GDF,
truncate_traces=True,
circular_target_area=False,
determine_branches_nodes=True,
Expand All @@ -151,10 +107,10 @@ def remote_or_local_read_file(url: str, path: Path):
azimuth_set_ranges=((135, 45), (45, 135)),
)

lidar_200k_network = Network(
LIDAR_200K_NETOWORK = Network(
name="1_200_000",
trace_gdf=lidar200k_trace_gdf,
area_gdf=lidar200k_area_gdf,
trace_gdf=LIDAR_200K_TRACE_GDF,
area_gdf=LIDAR_200K_AREA_GDF,
truncate_traces=True,
circular_target_area=False,
determine_branches_nodes=True,
Expand All @@ -163,3 +119,5 @@ def remote_or_local_read_file(url: str, path: Path):
azimuth_set_names=("N-S", "E-W"),
azimuth_set_ranges=((135, 45), (45, 135)),
)

KB11_IMAGE_PATH = PROJECT_BASE_PATH / "docs_src/imgs/kb11_orthomosaic.jpg"
11 changes: 5 additions & 6 deletions examples/fractopo_workflow_visualisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import Callable, Optional, Tuple

import matplotlib.pyplot as plt
from example_networks import kb11_network
from example_data import KB11_IMAGE_PATH, KB11_NETWORK
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from matplotlib.patheffects import withStroke
Expand All @@ -31,7 +31,7 @@ def wrapper(*args, **kwargs):
return wrapper


def plot_area(kb11_network: Network = kb11_network) -> Figure:
def plot_area(kb11_network: Network = KB11_NETWORK) -> Figure:
"""
Plot area boundary.
"""
Expand All @@ -43,7 +43,7 @@ def plot_area(kb11_network: Network = kb11_network) -> Figure:
return area_fig


def plot_traces_and_area(kb11_network: Network = kb11_network) -> Figure:
def plot_traces_and_area(kb11_network: Network = KB11_NETWORK) -> Figure:
"""
Plot area boundary along with the traces.
"""
Expand All @@ -57,7 +57,7 @@ def plot_traces_and_area(kb11_network: Network = kb11_network) -> Figure:
return area_fig


def plot_branches_and_area(kb11_network: Network = kb11_network) -> Figure:
def plot_branches_and_area(kb11_network: Network = KB11_NETWORK) -> Figure:
"""
Plot area boundary along with the branches.
"""
Expand Down Expand Up @@ -88,8 +88,7 @@ def plot_ortho(ax: Axes):
Plot drone orthomosaic.
"""
# Ortho
image_path = Path(__file__).parent.parent / "docs_src/imgs/kb11_orthomosaic.jpg"
with Image.open(image_path) as image:
with Image.open(KB11_IMAGE_PATH) as image:
# image = image.convert("RGBA")
# data = np.ones((image.size[1], image.size[0], 4), dtype=np.uint8) * 255
# mask = Image.fromarray(data)
Expand Down
12 changes: 6 additions & 6 deletions examples/plot_azimuth_set_relationships.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import matplotlib as mpl
import matplotlib.pyplot as plt

# Load kb11_network network from examples/example_networks.py
from example_networks import kb11_network
# Load kb11_network network from examples/example_data.py
from example_data import KB11_NETWORK

mpl.rcParams["figure.figsize"] = (5, 5)
mpl.rcParams["font.size"] = 8
Expand All @@ -24,16 +24,16 @@
#
# Azimuth sets (set by user):

pprint((kb11_network.azimuth_set_names, kb11_network.azimuth_set_ranges))
pprint((KB11_NETWORK.azimuth_set_names, KB11_NETWORK.azimuth_set_ranges))

# %%
# Visualize the relationships with a plot.

figs, _ = kb11_network.plot_azimuth_crosscut_abutting_relationships()
figs, _ = KB11_NETWORK.plot_azimuth_crosscut_abutting_relationships()

# Edit the figure to better fit the gallery webpage
figs[0].suptitle(
kb11_network.name,
KB11_NETWORK.name,
fontsize="large",
fontweight="bold",
fontfamily="DejaVu Sans",
Expand All @@ -45,4 +45,4 @@
# The relationships are also accessible in numerical form as a ``pandas``
# DataFrame.

pprint(kb11_network.azimuth_set_relationships)
pprint(KB11_NETWORK.azimuth_set_relationships)
8 changes: 4 additions & 4 deletions examples/plot_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# Import matplotlib for plotting
import matplotlib.pyplot as plt
from example_networks import kb11_network
from example_data import KB11_NETWORK

# Name the dataset
name = "KB11"
Expand All @@ -25,11 +25,11 @@
fig, ax = plt.subplots(figsize=(9, 9))

# Plot the loaded trace dataset consisting of fracture traces.
kb11_network.trace_gdf.plot(ax=ax, color="blue")
KB11_NETWORK.trace_gdf.plot(ax=ax, color="blue")

# Plot the loaded area dataset that consists of a single polygon
# that delineates the traces.
kb11_network.area_gdf.boundary.plot(ax=ax, color="red")
KB11_NETWORK.area_gdf.boundary.plot(ax=ax, color="red")

# Give the figure a title
ax.set_title(f"{name}, Coordinate Reference System = {kb11_network.trace_gdf.crs}")
ax.set_title(f"{name}, Coordinate Reference System = {KB11_NETWORK.trace_gdf.crs}")
22 changes: 11 additions & 11 deletions examples/plot_length_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import matplotlib as mpl
import matplotlib.pyplot as plt

# Load kb11_network network from examples/example_networks.py
from example_networks import kb11_network
# Load kb11_network network from examples/example_data.py
from example_data import KB11_NETWORK

mpl.rcParams["figure.figsize"] = (5, 5)
mpl.rcParams["font.size"] = 8
Expand All @@ -26,7 +26,7 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Log-log plot of network trace length distribution
fit, fig, ax = kb11_network.plot_trace_lengths()
fit, fig, ax = KB11_NETWORK.plot_trace_lengths()

# Use matplotlib helpers to make sure plot fits in the gallery webpage!
# (Not required.)
Expand All @@ -36,7 +36,7 @@
# %%

# Log-log plot of network branch length distribution
kb11_network.plot_branch_lengths()
KB11_NETWORK.plot_branch_lengths()
plt.tight_layout()
plt.show()

Expand All @@ -45,7 +45,7 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Log-log plot of network trace length distribution
kb11_network.plot_trace_lengths(use_probability_density_function=True)
KB11_NETWORK.plot_trace_lengths(use_probability_density_function=True)

# Use matplotlib helpers to make sure plot fits in the gallery webpage!
# (Not required.)
Expand All @@ -55,7 +55,7 @@
# %%

# Log-log plot of network branch length distribution
kb11_network.plot_branch_lengths(use_probability_density_function=True)
KB11_NETWORK.plot_branch_lengths(use_probability_density_function=True)
plt.tight_layout()
plt.show()

Expand All @@ -64,19 +64,19 @@
# -----------------------------------------------------------

# Use pprint for printing with prettier output
pprint(kb11_network.trace_lengths_powerlaw_fit_description)
pprint(KB11_NETWORK.trace_lengths_powerlaw_fit_description)

# %%

pprint(kb11_network.branch_lengths_powerlaw_fit_description)
pprint(KB11_NETWORK.branch_lengths_powerlaw_fit_description)

# %%
# Set-wise length distribution plotting
# -----------------------------------------------------------

pprint(kb11_network.azimuth_set_names)
pprint(kb11_network.azimuth_set_ranges)
pprint(KB11_NETWORK.azimuth_set_names)
pprint(KB11_NETWORK.azimuth_set_ranges)

# %%

fits, figs, axes = kb11_network.plot_trace_azimuth_set_lengths()
fits, figs, axes = KB11_NETWORK.plot_trace_azimuth_set_lengths()
4 changes: 2 additions & 2 deletions examples/plot_multi_scale_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import matplotlib.pyplot as plt

# Load kb11_network and hastholmen_network
from example_networks import hastholmen_network, kb11_network
from example_data import HASTHOLMEN_NETWORK, KB11_NETWORK

from fractopo import MultiNetwork

Expand All @@ -21,7 +21,7 @@
# Create MultiNetwork object
# ------------------------------------------------------------------

multi_network = MultiNetwork((kb11_network, hastholmen_network))
multi_network = MultiNetwork((KB11_NETWORK, HASTHOLMEN_NETWORK))

# %%
# Plot automatically cut length distributions with a multi-scale fit
Expand Down
8 changes: 4 additions & 4 deletions examples/plot_numerical_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import matplotlib.pyplot as plt

# Load kb11_network network from examples/example_networks.py
from example_networks import kb7_network, kb11_network
# Load kb11_network network from examples/example_data.py
from example_data import KB7_NETWORK, KB11_NETWORK

# Import Network class from fractopo
from fractopo.analysis.parameters import plot_parameters_plot
Expand All @@ -26,8 +26,8 @@
#
# All parameters are accessible through an attribute

kb11_parameters = kb11_network.parameters
kb7_parameters = kb7_network.parameters
kb11_parameters = KB11_NETWORK.parameters
kb7_parameters = KB7_NETWORK.parameters

# %%
pprint(kb11_parameters)
Expand Down
Loading

0 comments on commit 09e527a

Please sign in to comment.