From 15ba26f64965c27688a401360f44cc73170f7c42 Mon Sep 17 00:00:00 2001 From: Sung Bae Date: Mon, 20 Sep 2021 13:09:06 +1200 Subject: [PATCH 1/5] Committing to avoid local conflict --- qcore/configs/machine_local.json | 1 + qcore/im.py | 2 +- qcore/srf.py | 18 ++++++++++++++---- qcore/testing.py | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/qcore/configs/machine_local.json b/qcore/configs/machine_local.json index a23b6094..07887b87 100644 --- a/qcore/configs/machine_local.json +++ b/qcore/configs/machine_local.json @@ -1,4 +1,5 @@ { + "GMT_DATA" : "/Users/sungbae/PlottingData", "tools_dir" : "/nesi/project/nesi00213/tools", "cores_per_node" : 1, "MAX_NODES_PER_JOB" : 1, diff --git a/qcore/im.py b/qcore/im.py index 2856eb90..a9775bb2 100644 --- a/qcore/im.py +++ b/qcore/im.py @@ -139,7 +139,7 @@ def get_im_name(self): if self.period: return f"{self.name}_{self.period}" else: - return self.name + return f"{self.name}" def pretty_im_name(self): """ diff --git a/qcore/srf.py b/qcore/srf.py index 4818c8b8..28705e61 100644 --- a/qcore/srf.py +++ b/qcore/srf.py @@ -11,15 +11,18 @@ from math import ceil, cos, floor, radians, sqrt, sin, degrees, atan from subprocess import Popen, PIPE +import sys import numpy as np try: import alphashape + import shapely except ImportError: # only used for get_perimeter function pass + from qcore.binary_version import get_unversioned_bin # assumption that all srf files contain 6 values per line @@ -824,15 +827,22 @@ def get_perimeter(srf_file, depth=True, plot=False): # alpha=600 worked fine with SRF, roughness 0.1 # 1000 was cutting into the plane and missing points entirely # 800 was zigzagging a bit too much along the edge - try: - ashape = alphashape.alphashape(points, 600.0) - except NameError: + if not 'alphashape' in sys.modules: raise ImportError("install alphashape") + if not 'shapely' in sys.modules: + raise ImportError("install shapely") + + ashape = alphashape.alphashape( + points, + lambda ind, r: 1.0 + any(np.array(points)[ind][:, 0] == 0.0)) + + #ashape = alphashape.alphashape(points) + perimeters.append(np.dstack(ashape.exterior.coords.xy)[0]) if plot: fig, ax = plt.subplots() ax.scatter(*zip(*points)) - ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2)) + ax.add_patch(PolygonPatch(ashape, alpha=0.2)) plt.show() plt.close() diff --git a/qcore/testing.py b/qcore/testing.py index f06a3d3e..6986156c 100644 --- a/qcore/testing.py +++ b/qcore/testing.py @@ -51,7 +51,7 @@ def test_set_up(realizations): sys.exit("{} failed to extract data folder".format(err)) else: - print("Benchmark data folder already exits: ", data_store_path) + print("Benchmark data folder already exists: ", data_store_path) print(test_data_save_dirs) # Run all tests return test_data_save_dirs From 307f8b7531d9f59d7d8d4fbd2fe3cca07541bdff Mon Sep 17 00:00:00 2001 From: Sung Bae Date: Mon, 20 Sep 2021 17:12:32 +1200 Subject: [PATCH 2/5] Fixed alpha parameter to be optimized with a practical consideration --- qcore/srf.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/qcore/srf.py b/qcore/srf.py index 28705e61..3ac95605 100644 --- a/qcore/srf.py +++ b/qcore/srf.py @@ -17,6 +17,7 @@ try: import alphashape + from alphashape import optimizealpha import shapely except ImportError: # only used for get_perimeter function @@ -824,19 +825,19 @@ def get_perimeter(srf_file, depth=True, plot=False): ndip = planes[i]["ndip"] points = np.array([get_lonlat(sf, value=None) for j in range(ndip * nstk)]) - # alpha=600 worked fine with SRF, roughness 0.1 - # 1000 was cutting into the plane and missing points entirely - # 800 was zigzagging a bit too much along the edge - if not 'alphashape' in sys.modules: + if not "alphashape" in sys.modules: raise ImportError("install alphashape") - if not 'shapely' in sys.modules: + if not "shapely" in sys.modules: raise ImportError("install shapely") - ashape = alphashape.alphashape( - points, - lambda ind, r: 1.0 + any(np.array(points)[ind][:, 0] == 0.0)) + # The value of alpha parameter determines how tightly points are enclosed + # alpha= 0 means we get a convex-hull, but often a concave-hull represents a better fit. + # Viktor reported alpha=600 worked ok with SRF (roughness 0.1) but it is too high and often misses points entirely. + # if no alpha is given, the optimal value is to be found, but is impractically slow. + # The following will try to optimize alpha with 10 iterations (default is 1000), and if no success, alpha=0 (convex hull) - #ashape = alphashape.alphashape(points) + alpha = optimizealpha(points, max_iterations=10) + ashape = alphashape.alphashape(points, alpha) perimeters.append(np.dstack(ashape.exterior.coords.xy)[0]) if plot: From 9300c5922492880d7674047730a7b90cb40f8efb Mon Sep 17 00:00:00 2001 From: Sung Bae Date: Mon, 20 Sep 2021 17:22:59 +1200 Subject: [PATCH 3/5] Removed irrelevant changes --- qcore/configs/machine_local.json | 1 - qcore/im.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/qcore/configs/machine_local.json b/qcore/configs/machine_local.json index 07887b87..a23b6094 100644 --- a/qcore/configs/machine_local.json +++ b/qcore/configs/machine_local.json @@ -1,5 +1,4 @@ { - "GMT_DATA" : "/Users/sungbae/PlottingData", "tools_dir" : "/nesi/project/nesi00213/tools", "cores_per_node" : 1, "MAX_NODES_PER_JOB" : 1, diff --git a/qcore/im.py b/qcore/im.py index a9775bb2..2856eb90 100644 --- a/qcore/im.py +++ b/qcore/im.py @@ -139,7 +139,7 @@ def get_im_name(self): if self.period: return f"{self.name}_{self.period}" else: - return f"{self.name}" + return self.name def pretty_im_name(self): """ From d9e94e35db18b73a95b8401c1e7df91604f751e6 Mon Sep 17 00:00:00 2001 From: Sung Bae Date: Tue, 21 Sep 2021 14:35:44 +1200 Subject: [PATCH 4/5] Removed import error handling for alphashape --- qcore/srf.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/qcore/srf.py b/qcore/srf.py index 3ac95605..5ba33212 100644 --- a/qcore/srf.py +++ b/qcore/srf.py @@ -15,14 +15,7 @@ import numpy as np -try: - import alphashape - from alphashape import optimizealpha - import shapely -except ImportError: - # only used for get_perimeter function - pass - +from alphashape import alphashape, optimizealpha from qcore.binary_version import get_unversioned_bin @@ -837,9 +830,9 @@ def get_perimeter(srf_file, depth=True, plot=False): # The following will try to optimize alpha with 10 iterations (default is 1000), and if no success, alpha=0 (convex hull) alpha = optimizealpha(points, max_iterations=10) - ashape = alphashape.alphashape(points, alpha) - + ashape = alphashape(points, alpha) perimeters.append(np.dstack(ashape.exterior.coords.xy)[0]) + if plot: fig, ax = plt.subplots() ax.scatter(*zip(*points)) From a4dc8fed3428b538a9cca80953ecca77aead9415 Mon Sep 17 00:00:00 2001 From: Sung Bae Date: Tue, 21 Sep 2021 14:49:57 +1200 Subject: [PATCH 5/5] Removed ImportError handling 2 --- qcore/srf.py | 5 ----- requirements.txt | 1 + 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/qcore/srf.py b/qcore/srf.py index 5ba33212..acc83d18 100644 --- a/qcore/srf.py +++ b/qcore/srf.py @@ -818,11 +818,6 @@ def get_perimeter(srf_file, depth=True, plot=False): ndip = planes[i]["ndip"] points = np.array([get_lonlat(sf, value=None) for j in range(ndip * nstk)]) - if not "alphashape" in sys.modules: - raise ImportError("install alphashape") - if not "shapely" in sys.modules: - raise ImportError("install shapely") - # The value of alpha parameter determines how tightly points are enclosed # alpha= 0 means we get a convex-hull, but often a concave-hull represents a better fit. # Viktor reported alpha=600 worked ok with SRF (roughness 0.1) but it is too high and often misses points entirely. diff --git a/requirements.txt b/requirements.txt index c700201a..8bd817e0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pytest pytest-black +alphashape