Skip to content

Commit

Permalink
Merge pull request #132 from localdevices/issue_128
Browse files Browse the repository at this point in the history
Issue 128
  • Loading branch information
hcwinsemius authored Jun 27, 2023
2 parents c369197 + a71790a commit f687ba6
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 24 deletions.
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
## [0.5.1] - 2023-06-27
### Added
### Changed
### Deprecated
### Removed
### Fixed
- removed the strict cartopy dependency. This enables pip installation for users that are not interested in
geographical plotting. Enables also installation on raspi platforms (only 64-bit!)
- Transects sometimes gave infinite discharge when areas with zero depth received a small velocity. This has now
been resolved.
### Security


## [0.5.0] - 2023-05-24
### Added
- make it a lot easier to get well-calibrated ground control and lens parameters at the same time. we now do this
by optimizing the lens' focal length and (if enough ground control is provided) barrel distortion whilst fitting
the perspective to the user-provided ground control points.
- provide the fitted ground control points in the interface so that the user can immediately see if the ground control
points are well fitted or if anything seems to be wrong with one or more control points.
- feature stabilization on command line which consequently provided user-interfacing to select non-moving areas by
pointing and clicking.
### Changed
- Much-improved stabilization for non-stable videos
- stabilization can also be configured in CameraConfig level to accomodate slightly moving fixed rigs
- h_a can be provided on command-line instead of in recipe
### Deprecated
### Removed
- old velocimetry.filter_... methods are now entirely removed
### Fixed
### Security


## [0.4.0] - 2023-03-10
### Added
The most notable change is that the code now includes an automatically installed command-line interface. This
Expand Down
2 changes: 1 addition & 1 deletion pyorc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.5.0"
__version__ = "0.5.1"
from .api.cameraconfig import CameraConfig, load_camera_config, get_camera_config
from .api.video import Video
from .api.frames import Frames
Expand Down
4 changes: 2 additions & 2 deletions pyorc/api/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def get_piv(self, **kwargs):
coords_drop = list(set(self._obj.coords) - set(self._obj.dims))
obj = self._obj.drop_vars(coords_drop)
# get frames and shifted frames in time
frames1 = obj.shift(time=1)[1:].chunk({"time": 10})
frames2 = obj[1:].chunk({"time": 10})
frames1 = obj.shift(time=1)[1:].chunk({"time": 1})
frames2 = obj[1:].chunk({"time": 1})

# get the cols and rows coordinates of the expected results
cols, rows = piv_process.get_piv_size(
Expand Down
11 changes: 11 additions & 0 deletions pyorc/api/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ def get_plot_method(
"""
# in case persistent transform is in kwargs_line, remove this
if mode == "geographical":
try:
import cartopy
except:
raise ImportError("Cartopy not found, please install with 'mamba install -c conda-forge cartopy")
if "transform" in kwargs_line:
del kwargs_line["transform"]
ax = _prepare_axes(ax=ax, mode=mode)
Expand Down Expand Up @@ -185,6 +190,7 @@ def get_plot_method(
return get_plot_method



def _frames_plot(ref, ax=None, mode="local", *args, **kwargs):
"""Creates QuadMesh plot from a RGB or grayscale frame on a new or existing (if ax is not None) axes
Expand All @@ -207,6 +213,11 @@ def _frames_plot(ref, ax=None, mode="local", *args, **kwargs):
"""
# prepare axes
if mode == "geographical":
try:
import cartopy
except:
raise ImportError("Cartopy not found, please install with 'mamba install -c conda-forge cartopy")
if "time" in ref._obj.coords:
if ref._obj.time.size > 1:
raise AttributeError(f'Object contains dimension "time" with length {len(ref._obj.time)}. Reduce dataset by selecting one time step or taking a median, mean or other statistic.')
Expand Down
31 changes: 19 additions & 12 deletions pyorc/cli/cli_elements.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
try:
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
use_cartopy = True
except:
use_cartopy = False

import logging


import click
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -28,19 +34,20 @@ class BaseSelect:
def __init__(self, img, dst=None, crs=None, buffer=0.0002, zoom_level=19, logger=logging):
self.logger = logger
self.height, self.width = img.shape[0:2]
self.crs = crs
if use_cartopy:
self.crs = crs
else:
self.crs = None
fig = plt.figure(figsize=(16, 9), frameon=False, facecolor="black")
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
ax_geo = None
# extent = [4.5, 4.51, 51.2, 51.21]
if dst is not None:
# fig = plt.figure(figsize=(12, 7))
xmin = np.array(dst)[:, 0].min()
xmax = np.array(dst)[:, 0].max()
ymin = np.array(dst)[:, 1].min()
ymax = np.array(dst)[:, 1].max()
extent = [xmin - buffer, xmax + buffer, ymin - buffer, ymax + buffer]
if crs is not None:
if self.crs is not None:
tiler = getattr(cimgt, "GoogleTiles")(style="satellite")
ax_geo = fig.add_axes([0., 0., 1, 1], projection=tiler.crs)
ax_geo.set_extent(extent, crs=ccrs.PlateCarree())
Expand Down Expand Up @@ -75,7 +82,7 @@ def __init__(self, img, dst=None, crs=None, buffer=0.0002, zoom_level=19, logger
],
)
if dst is not None:
if crs is not None:
if self.crs is not None:
kwargs["transform"] = ccrs.PlateCarree()
transform = ccrs.PlateCarree()._as_mpl_transform(ax_geo)
kwargs_text["xycoords"] = transform
Expand Down Expand Up @@ -276,15 +283,15 @@ def __init__(self, img, src, dst, camera_config, logger=logging):
markeredgecolor="w",
zorder=3,
)
if hasattr(self.camera_config, "crs"):
if hasattr(self.camera_config, "crs") and use_cartopy:
kwargs["transform"] = ccrs.PlateCarree()
self.p_geo, = self.ax_geo.plot(
[], [], "o",
**kwargs
)
# plot an empty polygon
pol = Polygon(np.zeros((0, 2)), edgecolor="w", alpha=0.5, linewidth=2)
if hasattr(self.camera_config, "crs"):
if hasattr(self.camera_config, "crs") and use_cartopy:
pol_geo = Polygon(np.zeros((0, 2)), edgecolor="w", alpha=0.5, linewidth=2, transform=ccrs.PlateCarree(),
zorder=3)
else:
Expand Down Expand Up @@ -332,7 +339,7 @@ def on_left_click(self, event):
self.camera_config.set_bbox_from_corners(self.src)
bbox_cam = list(zip(*self.camera_config.get_bbox(camera=True, redistort=True).exterior.xy))
bbox_geo = list(zip(*self.camera_config.get_bbox().exterior.xy))
if hasattr(self.camera_config, "crs"):
if hasattr(self.camera_config, "crs") and use_cartopy:
bbox_geo = helpers.xyz_transform(
bbox_geo,
crs_from=self.camera_config.crs,
Expand Down Expand Up @@ -368,7 +375,7 @@ def __init__(self, img, dst, crs=None, lens_position=None, logger=logging):
markersize=10,
label="Selected control points"
)
if crs is not None:
if crs is not None and use_cartopy:
kwargs["transform"] = ccrs.PlateCarree()
self.p_geo_selected, = self.ax_geo.plot(
[], [], "o",
Expand Down Expand Up @@ -399,7 +406,7 @@ def __init__(self, img, dst, crs=None, lens_position=None, logger=logging):
self.ax.legend()
self.lens_position = lens_position
# add dst coords in the intended CRS
if crs is not None:
if crs is not None and use_cartopy:
self.dst_crs = helpers.xyz_transform(self.dst, 4326, crs)
else:
self.dst_crs = self.dst
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
setup(
name="pyopenrivercam",
description="pyopenrivercam (pyorc) is a front and backend to control river camera observation locations",
version="0.5.0",
version="0.5.1",
long_description=readme + "\n\n",
long_description_content_type="text/markdown",
url="https://github.com/localdevices/pyorc",
Expand All @@ -28,6 +28,7 @@
"dask",
"descartes",
"geojson",
"geopandas",
"matplotlib",
"netCDF4",
"numpy",
Expand Down
12 changes: 6 additions & 6 deletions tests/test_cameraconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pyorc
import pytest

from cartopy.mpl.geoaxes import GeoAxesSubplot
# from cartopy.mpl.geoaxes import GeoAxesSubplot
from pyorc import helpers, cv

from shapely.geometry import Polygon
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_load_camera_config(cam_config_fn, cam_config, lens_position):
"camera",
[
True,
False
# False
]
)
def test_plot(cam_config, vid, camera):
Expand All @@ -161,10 +161,10 @@ def test_plot(cam_config, vid, camera):
assert(
isinstance(ax, matplotlib.axes.Axes)
)
else:
assert(
isinstance(ax, GeoAxesSubplot)
)
#else:
#assert(
#isinstance(ax, GeoAxesSubplot)
#)


def test_cv_undistort_points(cam_config):
Expand Down
12 changes: 10 additions & 2 deletions tests/test_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,16 @@ def test_plot(frames, idx):
)
def test_plot_proj(frames_proj, idx):
frames_proj[idx].frames.plot()
frames_proj[idx].frames.plot(mode="geographical")
plt.show(block=False)
plt.close("all")
try:
import cartopy
frames_proj[idx].frames.plot(mode="geographical")
plt.show(block=False)
plt.close("all")
except:
print("Cartopy is missing, skipping cartopy dependent test")



@pytest.mark.parametrize(
Expand Down Expand Up @@ -116,4 +124,4 @@ def test_to_ani(frames, ani_mp4):
)
def test_to_video(frames, ani_mp4):
# only store the first 3 frames
frames[0:3].frames.to_video(ani_mp4)
frames[0:3].frames.to_video(ani_mp4)
7 changes: 7 additions & 0 deletions tests/test_velocimetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ def test_get_transect(piv, cross_section, distance, nr_points):
)
def test_plot(piv, mode, method):
plot = True
if mode == "geographical":
try:
import cartopy
except:
print("Cartopy is missing, skipping cartopy dependent test")
plot = False
if method == "streamplot":
if mode != "local":
# skipping the test, because streamplot only works local
plot = False
if plot:
piv.mean(dim="time", keep_attrs=True).velocimetry.plot(method=method, mode=mode)
plt.show(block=False)
plt.close("all")

0 comments on commit f687ba6

Please sign in to comment.