curve-apps 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- LICENSE +21 -0
- README.rst +163 -0
- curve_apps/__init__.py +23 -0
- curve_apps/driver.py +103 -0
- curve_apps/edge_detection/__init__.py +5 -0
- curve_apps/edge_detection/driver.py +211 -0
- curve_apps/edge_detection/params.py +82 -0
- curve_apps/params.py +27 -0
- curve_apps/trend_lines/__init__.py +5 -0
- curve_apps/trend_lines/driver.py +186 -0
- curve_apps/trend_lines/params.py +67 -0
- curve_apps-0.1.0.dist-info/LICENSE +21 -0
- curve_apps-0.1.0.dist-info/METADATA +205 -0
- curve_apps-0.1.0.dist-info/RECORD +21 -0
- curve_apps-0.1.0.dist-info/WHEEL +4 -0
- curve_apps-0.1.0.dist-info/entry_points.txt +0 -0
- curve_apps-assets/__init__.py +5 -0
- curve_apps-assets/curve_apps_demo.geoh5 +0 -0
- curve_apps-assets/uijson/edge_detection.ui.json +107 -0
- curve_apps-assets/uijson/trend_lines.ui.json +120 -0
- docs/THIRD_PARTY_SOFTWARE.rst +28 -0
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-2024 Mira Geoscience
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
README.rst
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
|coverage| |maintainability| |precommit_ci| |docs| |style| |version| |status| |pyversions|
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
.. |docs| image:: https://readthedocs.org/projects/curve-apps/badge/
|
|
5
|
+
:alt: Documentation Status
|
|
6
|
+
:target: https://curve-apps.readthedocs.io/en/latest/?badge=latest
|
|
7
|
+
|
|
8
|
+
.. |coverage| image:: https://codecov.io/gh/MiraGeoscience/curve-apps/branch/develop/graph/badge.svg
|
|
9
|
+
:alt: Code coverage
|
|
10
|
+
:target: https://codecov.io/gh/MiraGeoscience/curve-apps
|
|
11
|
+
|
|
12
|
+
.. |style| image:: https://img.shields.io/badge/code%20style-black-000000.svg
|
|
13
|
+
:alt: Coding style
|
|
14
|
+
:target: https://github.com/pf/black
|
|
15
|
+
|
|
16
|
+
.. |version| image:: https://img.shields.io/pypi/v/edge-detection.svg
|
|
17
|
+
:alt: version on PyPI
|
|
18
|
+
:target: https://pypi.python.org/pypi/edge-detection/
|
|
19
|
+
|
|
20
|
+
.. |status| image:: https://img.shields.io/pypi/status/edge-detection.svg
|
|
21
|
+
:alt: version status on PyPI
|
|
22
|
+
:target: https://pypi.python.org/pypi/edge-detection/
|
|
23
|
+
|
|
24
|
+
.. |pyversions| image:: https://img.shields.io/pypi/pyversions/edge-detection.svg
|
|
25
|
+
:alt: Python versions
|
|
26
|
+
:target: https://pypi.python.org/pypi/edge-detection/
|
|
27
|
+
|
|
28
|
+
.. |precommit_ci| image:: https://results.pre-commit.ci/badge/github/MiraGeoscience/edge-detection/develop.svg
|
|
29
|
+
:alt: pre-commit.ci status
|
|
30
|
+
:target: https://results.pre-commit.ci/latest/github/MiraGeoscience/edge-detection/develop
|
|
31
|
+
|
|
32
|
+
.. |maintainability| image:: https://api.codeclimate.com/v1/badges/_token_/maintainability
|
|
33
|
+
:target: https://codeclimate.com/github/MiraGeoscience/edge-detection/maintainability
|
|
34
|
+
:alt: Maintainability
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
curve-apps
|
|
38
|
+
==========
|
|
39
|
+
|
|
40
|
+
Auto-detection of trends and edges in geoscientific data.
|
|
41
|
+
|
|
42
|
+
.. contents:: Table of Contents
|
|
43
|
+
:local:
|
|
44
|
+
:depth: 3
|
|
45
|
+
|
|
46
|
+
Documentation
|
|
47
|
+
^^^^^^^^^^^^^
|
|
48
|
+
`Online documentation <https://edge-detection.readthedocs.io/en/latest/>`_
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
Installation
|
|
52
|
+
^^^^^^^^^^^^
|
|
53
|
+
**curve-apps** is currently written and tested for Python 3.10.
|
|
54
|
+
|
|
55
|
+
Install Conda
|
|
56
|
+
-------------
|
|
57
|
+
|
|
58
|
+
To install **curve-apps**, you need to install **Conda** first.
|
|
59
|
+
|
|
60
|
+
We recommend to install **Conda** using `miniforge`_.
|
|
61
|
+
|
|
62
|
+
.. _miniforge: https://github.com/conda-forge/miniforge
|
|
63
|
+
|
|
64
|
+
Within a conda environment
|
|
65
|
+
--------------------------
|
|
66
|
+
|
|
67
|
+
You can install (or update) a conda environment with all the requires packages to run **curve-apps**.
|
|
68
|
+
To do so you can directly run the **Install_or_Update.bat** file by double left clicking on it.
|
|
69
|
+
|
|
70
|
+
Install with conda
|
|
71
|
+
------------------
|
|
72
|
+
|
|
73
|
+
You can install the package using ``conda`` and the ``.lock`` files from a conda prompt:
|
|
74
|
+
|
|
75
|
+
.. code-block:: bash
|
|
76
|
+
|
|
77
|
+
conda env create -n my-env -f environments/[the_desired_env].lock.yml
|
|
78
|
+
|
|
79
|
+
Install with PyPI
|
|
80
|
+
-----------------
|
|
81
|
+
|
|
82
|
+
You should not install the package from PyPI, as the app requires conda packages to run.
|
|
83
|
+
Still, you can install it in a conda environment without its dependencies (``--no-deps``).
|
|
84
|
+
|
|
85
|
+
From PyPI
|
|
86
|
+
~~~~~~~~~
|
|
87
|
+
|
|
88
|
+
To install the **curve-apps** package published on PyPI:
|
|
89
|
+
|
|
90
|
+
.. code-block:: bash
|
|
91
|
+
|
|
92
|
+
pip install -U --no-deps curve-apps
|
|
93
|
+
|
|
94
|
+
From a Git tag or branch
|
|
95
|
+
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
96
|
+
If the package is not on PiPY yet, you can install it from a Git tag:
|
|
97
|
+
|
|
98
|
+
.. code-block:: bash
|
|
99
|
+
|
|
100
|
+
pip install -U --no-deps --force-reinstall https://github.com/MiraGeoscience/curve-apps/archive/refs/tags/TAG.zip
|
|
101
|
+
|
|
102
|
+
Or to install the latest changes available on a given Git branch:
|
|
103
|
+
|
|
104
|
+
.. code-block:: bash
|
|
105
|
+
|
|
106
|
+
pip install -U --no-deps --force-reinstall https://github.com/MiraGeoscience/curve-apps/archive/refs/heads/BRANCH.zip
|
|
107
|
+
|
|
108
|
+
.. note::
|
|
109
|
+
The ``--force-reinstall`` option is used to make sure the updated version
|
|
110
|
+
of the sources is installed, and not the cached version, even if the version number
|
|
111
|
+
did not change. The ``-U`` or ``--upgrade`` option is used to make sure to get the latest version,
|
|
112
|
+
on not merely reinstall the same version. As the package is aimed to be in a **Conda environment**, the option ``--no-deps`` is used to avoid installing the dependencies with pip, as they will be installed with conda.
|
|
113
|
+
|
|
114
|
+
From a local copy of the sources
|
|
115
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
116
|
+
If you have a git clone of the package sources locally,
|
|
117
|
+
you can install **curve-apps** from the local copy of the sources with:
|
|
118
|
+
|
|
119
|
+
.. code-block:: bash
|
|
120
|
+
|
|
121
|
+
pip install -U --force-reinstall path/to/project_folder_with_pyproject_toml
|
|
122
|
+
|
|
123
|
+
Or in **editable mode**, so that you can edit the sources and see the effect immediately at runtime:
|
|
124
|
+
|
|
125
|
+
.. code-block:: bash
|
|
126
|
+
|
|
127
|
+
pip install -e -U --force-reinstall path/to/project_folder_with_pyproject_toml
|
|
128
|
+
|
|
129
|
+
Setup for development
|
|
130
|
+
^^^^^^^^^^^^^^^^^^^^^
|
|
131
|
+
To configure the development environment and tools, please see `README-dev.rst`_.
|
|
132
|
+
|
|
133
|
+
.. _README-dev.rst: README-dev.rst
|
|
134
|
+
|
|
135
|
+
License
|
|
136
|
+
^^^^^^^
|
|
137
|
+
curve-apps is free software: you can redistribute it and/or modify
|
|
138
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
139
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
140
|
+
(at your option) any later version.
|
|
141
|
+
|
|
142
|
+
curve-apps is distributed in the hope that it will be useful,
|
|
143
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
144
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
145
|
+
GNU Lesser General Public License for more details.
|
|
146
|
+
|
|
147
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
148
|
+
along with curve-apps. If not, see <https://www.gnu.org/licenses/>.
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
Third Party Software
|
|
152
|
+
^^^^^^^^^^^^^^^^^^^^
|
|
153
|
+
The curve-apps Software may provide links to third party libraries or code (collectively “Third Party Software”)
|
|
154
|
+
to implement various functions. Third Party Software does not comprise part of the Software.
|
|
155
|
+
The use of Third Party Software is governed by the terms of such software license(s).
|
|
156
|
+
Third Party Software notices and/or additional terms and conditions are located in the
|
|
157
|
+
`THIRD_PARTY_SOFTWARE.rst`_ file.
|
|
158
|
+
|
|
159
|
+
.. _THIRD_PARTY_SOFTWARE.rst: ./docs/THIRD_PARTY_SOFTWARE.rst
|
|
160
|
+
|
|
161
|
+
Copyright
|
|
162
|
+
^^^^^^^^^
|
|
163
|
+
Copyright (c) 2024 Mira Geoscience Ltd.
|
curve_apps/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Copyright (c) 2024 Mira Geoscience Ltd.
|
|
2
|
+
#
|
|
3
|
+
# This file is part of curve-apps package.
|
|
4
|
+
#
|
|
5
|
+
# All rights reserved.
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
__version__ = "0.1.0"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def assets_path() -> Path:
|
|
15
|
+
"""Return the path to the assets folder."""
|
|
16
|
+
|
|
17
|
+
parent = Path(__file__).parent
|
|
18
|
+
folder_name = f"{parent.name}-assets"
|
|
19
|
+
assets_folder = parent.parent / folder_name
|
|
20
|
+
if not assets_folder.is_dir():
|
|
21
|
+
raise RuntimeError(f"Assets folder not found: {assets_folder}")
|
|
22
|
+
|
|
23
|
+
return assets_folder
|
curve_apps/driver.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Copyright (c) 2024 Mira Geoscience Ltd.
|
|
2
|
+
#
|
|
3
|
+
# This file is part of edge-detection package.
|
|
4
|
+
#
|
|
5
|
+
# All rights reserved.
|
|
6
|
+
#
|
|
7
|
+
#
|
|
8
|
+
# This file is part of curve-apps.
|
|
9
|
+
#
|
|
10
|
+
# curve-apps is distributed under the terms and conditions of the MIT License
|
|
11
|
+
# (see LICENSE file at the root of this source code package).
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import logging
|
|
17
|
+
import tempfile
|
|
18
|
+
from abc import abstractmethod
|
|
19
|
+
|
|
20
|
+
from geoapps_utils.driver.data import BaseData
|
|
21
|
+
from geoapps_utils.driver.driver import BaseDriver
|
|
22
|
+
from geoh5py.groups import ContainerGroup
|
|
23
|
+
from geoh5py.objects import ObjectBase
|
|
24
|
+
from geoh5py.ui_json import InputFile
|
|
25
|
+
|
|
26
|
+
logger = logging.getLogger(__name__)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class BaseCurveDriver(BaseDriver):
|
|
30
|
+
"""
|
|
31
|
+
Driver for the edge detection application.
|
|
32
|
+
|
|
33
|
+
:param parameters: Application parameters.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
_parameter_class: type[BaseData]
|
|
37
|
+
_default_name: str
|
|
38
|
+
|
|
39
|
+
def __init__(self, parameters: BaseData | InputFile):
|
|
40
|
+
if isinstance(parameters, InputFile):
|
|
41
|
+
parameters = self._parameter_class.build(parameters)
|
|
42
|
+
|
|
43
|
+
# TODO need to re-type params in base class
|
|
44
|
+
super().__init__(parameters)
|
|
45
|
+
|
|
46
|
+
def run(self):
|
|
47
|
+
"""
|
|
48
|
+
Run method of the driver.
|
|
49
|
+
"""
|
|
50
|
+
with self.workspace.open(mode="r+") as workspace:
|
|
51
|
+
parent = None
|
|
52
|
+
if self.params.output.out_group is not None:
|
|
53
|
+
parent = ContainerGroup.create(
|
|
54
|
+
workspace=workspace,
|
|
55
|
+
name=self.params.output.out_group,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
name = self.params.output.export_as
|
|
59
|
+
if name is None:
|
|
60
|
+
name = self._default_name
|
|
61
|
+
|
|
62
|
+
logger.info("Begin process.")
|
|
63
|
+
output = self.create_output(name, parent=parent)
|
|
64
|
+
logger.info("Process completed.")
|
|
65
|
+
|
|
66
|
+
if output is not None:
|
|
67
|
+
self.update_monitoring_directory(
|
|
68
|
+
parent if parent is not None else output
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
logger.info("Curve object '%s' saved to '%s'.", name, str(workspace.h5file))
|
|
72
|
+
|
|
73
|
+
@abstractmethod
|
|
74
|
+
def create_output(self, name: str, parent: ContainerGroup | None = None):
|
|
75
|
+
"""
|
|
76
|
+
Create output.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def params(self) -> BaseData:
|
|
81
|
+
"""Application parameters."""
|
|
82
|
+
return self._params
|
|
83
|
+
|
|
84
|
+
@params.setter
|
|
85
|
+
def params(self, val: BaseData):
|
|
86
|
+
if not isinstance(val, BaseData):
|
|
87
|
+
raise TypeError("Parameters must be of type Parameters.")
|
|
88
|
+
self._params = val
|
|
89
|
+
|
|
90
|
+
def add_ui_json(self, entity: ObjectBase | ContainerGroup):
|
|
91
|
+
"""
|
|
92
|
+
Add ui.json file to entity.
|
|
93
|
+
|
|
94
|
+
:param entity: Object to add ui.json file to.
|
|
95
|
+
"""
|
|
96
|
+
param_dict = self.params.flatten()
|
|
97
|
+
self.params.input_file.update_ui_values(param_dict)
|
|
98
|
+
|
|
99
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
|
100
|
+
file_path = self.params.input_file.write_ui_json(
|
|
101
|
+
path=temp_dir, name=self.params.name + ".ui.json"
|
|
102
|
+
)
|
|
103
|
+
entity.add_file(str(file_path))
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# Copyright (c) 2024 Mira Geoscience Ltd.
|
|
2
|
+
#
|
|
3
|
+
# This file is part of edge-detection package.
|
|
4
|
+
#
|
|
5
|
+
# All rights reserved.
|
|
6
|
+
#
|
|
7
|
+
#
|
|
8
|
+
# This file is part of curve-apps.
|
|
9
|
+
#
|
|
10
|
+
# curve-apps is distributed under the terms and conditions of the MIT License
|
|
11
|
+
# (see LICENSE file at the root of this source code package).
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import sys
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
from geoapps_utils.locations import get_overlapping_limits, map_indices_to_coordinates
|
|
20
|
+
from geoh5py.data import FloatData
|
|
21
|
+
from geoh5py.groups import ContainerGroup
|
|
22
|
+
from geoh5py.objects import Curve, Grid2D
|
|
23
|
+
from geoh5py.ui_json import InputFile, utils
|
|
24
|
+
from scipy.spatial import cKDTree
|
|
25
|
+
from skimage.feature import canny
|
|
26
|
+
from skimage.transform import probabilistic_hough_line
|
|
27
|
+
|
|
28
|
+
from ..driver import BaseCurveDriver
|
|
29
|
+
from .params import NAME, DetectionParameters, Parameters
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class EdgeDetectionDriver(BaseCurveDriver):
|
|
33
|
+
"""
|
|
34
|
+
Driver for the edge detection application.
|
|
35
|
+
|
|
36
|
+
:param parameters: Application parameters.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
_parameter_class = Parameters
|
|
40
|
+
_default_name = NAME
|
|
41
|
+
|
|
42
|
+
def __init__(self, parameters: Parameters | InputFile):
|
|
43
|
+
super().__init__(parameters)
|
|
44
|
+
|
|
45
|
+
def create_output(self, name, parent: ContainerGroup | None = None):
|
|
46
|
+
"""
|
|
47
|
+
Driver for Grid2D objects for the automated detection of line features.
|
|
48
|
+
The application relies on the Canny and Hough transforms from the
|
|
49
|
+
Scikit-Image library.
|
|
50
|
+
|
|
51
|
+
:param name: Name of the output object.
|
|
52
|
+
:param parent: Optional parent group.
|
|
53
|
+
"""
|
|
54
|
+
with utils.fetch_active_workspace(self.workspace) as workspace:
|
|
55
|
+
vertices, cells = EdgeDetectionDriver.get_edges(
|
|
56
|
+
self.params.source.objects,
|
|
57
|
+
self.params.source.data,
|
|
58
|
+
self.params.detection,
|
|
59
|
+
)
|
|
60
|
+
edges = Curve.create(
|
|
61
|
+
workspace=workspace,
|
|
62
|
+
name=name,
|
|
63
|
+
vertices=vertices,
|
|
64
|
+
cells=cells,
|
|
65
|
+
parent=parent,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# Compute positive angle from North
|
|
69
|
+
# TODO: Move to geoapps-utils
|
|
70
|
+
delta = np.c_[
|
|
71
|
+
vertices[cells[:, 1], 0] - vertices[cells[:, 0], 0],
|
|
72
|
+
vertices[cells[:, 1], 1] - vertices[cells[:, 0], 1],
|
|
73
|
+
]
|
|
74
|
+
delta[delta[:, 0] < 0, :] *= -1
|
|
75
|
+
amp = np.linalg.norm(delta, axis=1)
|
|
76
|
+
orientation = np.arccos(delta[:, 1] / amp)
|
|
77
|
+
|
|
78
|
+
# TODO: Assign values to vertices until better handling of cell data by GA
|
|
79
|
+
vert_azimuth = np.zeros(edges.n_vertices) * np.nan
|
|
80
|
+
vert_azimuth[cells.flatten()] = np.repeat(orientation, 2)
|
|
81
|
+
edges.add_data(
|
|
82
|
+
{
|
|
83
|
+
"azimuth": {"values": np.degrees(vert_azimuth)},
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
vert_lengths = np.zeros(edges.n_vertices) * np.nan
|
|
88
|
+
vert_lengths[cells.flatten()] = np.repeat(amp, 2)
|
|
89
|
+
edges.add_data(
|
|
90
|
+
{
|
|
91
|
+
"lengths": {"values": vert_lengths},
|
|
92
|
+
}
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
return edges
|
|
96
|
+
|
|
97
|
+
@staticmethod
|
|
98
|
+
def get_edges(
|
|
99
|
+
grid: Grid2D,
|
|
100
|
+
data: FloatData,
|
|
101
|
+
detection: DetectionParameters,
|
|
102
|
+
) -> tuple:
|
|
103
|
+
"""
|
|
104
|
+
Find edges in gridded data.
|
|
105
|
+
|
|
106
|
+
:params grid: A Grid2D object.
|
|
107
|
+
:params data: Input data.
|
|
108
|
+
:params detection: Detection parameters.
|
|
109
|
+
|
|
110
|
+
:returns : n x 3 array. Vertices of edges.
|
|
111
|
+
:returns : n x 2 float array. Cells of edges.
|
|
112
|
+
"""
|
|
113
|
+
if grid.shape is None or data.values is None:
|
|
114
|
+
return None, None
|
|
115
|
+
|
|
116
|
+
grid_data = data.values.reshape(grid.shape, order="F")
|
|
117
|
+
|
|
118
|
+
if np.all(np.isnan(grid_data)):
|
|
119
|
+
return None, None
|
|
120
|
+
|
|
121
|
+
# Find edges
|
|
122
|
+
edges = canny(
|
|
123
|
+
grid_data,
|
|
124
|
+
sigma=detection.sigma,
|
|
125
|
+
use_quantiles=True,
|
|
126
|
+
mask=~np.isnan(grid_data),
|
|
127
|
+
mode="reflect",
|
|
128
|
+
)
|
|
129
|
+
grid.add_data({"canny filter": {"values": edges.flatten(order="F")}})
|
|
130
|
+
|
|
131
|
+
# Find lines
|
|
132
|
+
indices = EdgeDetectionDriver.get_line_indices(
|
|
133
|
+
edges,
|
|
134
|
+
detection.line_length,
|
|
135
|
+
detection.line_gap,
|
|
136
|
+
detection.threshold,
|
|
137
|
+
detection.window_size,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
if len(indices) == 0:
|
|
141
|
+
return None, None
|
|
142
|
+
|
|
143
|
+
pixel_coordinates = np.vstack(indices)
|
|
144
|
+
|
|
145
|
+
if detection.merge_length is not None:
|
|
146
|
+
vertices = map_indices_to_coordinates(grid, pixel_coordinates)
|
|
147
|
+
tree = cKDTree(vertices)
|
|
148
|
+
merge = tree.query_pairs(detection.merge_length, output_type="ndarray")
|
|
149
|
+
pixel_coordinates[merge[:, 0], :] = pixel_coordinates[merge[:, 1], :]
|
|
150
|
+
|
|
151
|
+
u_ind, r_ind = np.unique(pixel_coordinates, axis=0, return_inverse=True)
|
|
152
|
+
vertices = map_indices_to_coordinates(grid, u_ind)
|
|
153
|
+
|
|
154
|
+
cells = r_ind.reshape((-1, 2))
|
|
155
|
+
return vertices, cells
|
|
156
|
+
|
|
157
|
+
@staticmethod
|
|
158
|
+
def get_line_indices(
|
|
159
|
+
canny_image: np.ndarray,
|
|
160
|
+
line_length: int = 1,
|
|
161
|
+
line_gap: int = 1,
|
|
162
|
+
threshold: int = 1,
|
|
163
|
+
window_size: int | None = None,
|
|
164
|
+
) -> list:
|
|
165
|
+
"""
|
|
166
|
+
Get indices forming lines on a canny image.
|
|
167
|
+
|
|
168
|
+
The process is done over tiles of square size. The tiles overlap by 25%.
|
|
169
|
+
|
|
170
|
+
:param canny_image: Edges.
|
|
171
|
+
:param line_length: Minimum accepted pixel length of detected lines. (Hough)
|
|
172
|
+
:param line_gap: Maximum gap between pixels to still form a line. (Hough)
|
|
173
|
+
:param threshold: Value threshold. (Hough)
|
|
174
|
+
:param window_size: Size of the window to search for lines.
|
|
175
|
+
|
|
176
|
+
:returns: List of indices.
|
|
177
|
+
"""
|
|
178
|
+
# Cycle through tiles of square size
|
|
179
|
+
width = np.min(canny_image.shape)
|
|
180
|
+
if window_size is not None:
|
|
181
|
+
width = np.min([window_size, width])
|
|
182
|
+
|
|
183
|
+
x_limits = get_overlapping_limits(canny_image.shape[0], width)
|
|
184
|
+
y_limits = get_overlapping_limits(canny_image.shape[1], width)
|
|
185
|
+
|
|
186
|
+
# TODO: Loop in parallel
|
|
187
|
+
indices = []
|
|
188
|
+
for x_lim in x_limits:
|
|
189
|
+
for y_lim in y_limits:
|
|
190
|
+
lines = probabilistic_hough_line(
|
|
191
|
+
canny_image[x_lim[0] : x_lim[1], y_lim[0] : y_lim[1]],
|
|
192
|
+
line_length=line_length,
|
|
193
|
+
threshold=threshold,
|
|
194
|
+
line_gap=line_gap,
|
|
195
|
+
seed=0,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
if np.any(lines):
|
|
199
|
+
# Add the limits of the tile to the indices
|
|
200
|
+
lines = np.vstack(lines)[:, ::-1] + np.c_[x_lim[0], y_lim[0]]
|
|
201
|
+
indices.append(lines)
|
|
202
|
+
|
|
203
|
+
return indices
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
if __name__ == "__main__":
|
|
207
|
+
file = sys.argv[1]
|
|
208
|
+
ifile = InputFile.read_ui_json(file)
|
|
209
|
+
|
|
210
|
+
driver = EdgeDetectionDriver(ifile)
|
|
211
|
+
driver.run()
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Copyright (c) 2024 Mira Geoscience Ltd.
|
|
2
|
+
#
|
|
3
|
+
# This file is part of edge-detection package.
|
|
4
|
+
#
|
|
5
|
+
# All rights reserved.
|
|
6
|
+
#
|
|
7
|
+
#
|
|
8
|
+
# This file is part of curve-apps.
|
|
9
|
+
#
|
|
10
|
+
# curve-apps is distributed under the terms and conditions of the MIT License
|
|
11
|
+
# (see LICENSE file at the root of this source code package).
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from geoapps_utils.driver.data import BaseData
|
|
16
|
+
from geoh5py.data import FloatData
|
|
17
|
+
from geoh5py.objects import Grid2D
|
|
18
|
+
from geoh5py.ui_json import InputFile
|
|
19
|
+
from pydantic import BaseModel, ConfigDict
|
|
20
|
+
|
|
21
|
+
from curve_apps import assets_path
|
|
22
|
+
|
|
23
|
+
from ..params import OutputParameters
|
|
24
|
+
|
|
25
|
+
NAME = "edge_detection"
|
|
26
|
+
DEFAULT_UI_JSON = assets_path() / f"uijson/{NAME}.ui.json"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class SourceParameters(BaseModel):
|
|
30
|
+
"""
|
|
31
|
+
Source parameters expected by the ui.json file format.
|
|
32
|
+
|
|
33
|
+
:param objects: A Grid2D source object.
|
|
34
|
+
:param data: Data values to find edges on.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
model_config = ConfigDict(arbitrary_types_allowed=True)
|
|
38
|
+
|
|
39
|
+
objects: Grid2D
|
|
40
|
+
data: FloatData
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class DetectionParameters(BaseModel):
|
|
44
|
+
"""
|
|
45
|
+
Detection parameters expected by the ui.json file format.
|
|
46
|
+
|
|
47
|
+
:param line_length: Minimum accepted pixel length of detected lines. (Hough)
|
|
48
|
+
:param line_gap: Maximum gap between pixels to still form a line. (Hough)
|
|
49
|
+
:param sigma: Standard deviation of the Gaussian filter. (Canny)
|
|
50
|
+
:param threshold: Value threshold. (Hough)
|
|
51
|
+
:param window_size: Size of the window to search for lines.
|
|
52
|
+
:param merge_length: Minimum length between nodes that should be merged.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
line_length: int = 1
|
|
56
|
+
line_gap: int = 1
|
|
57
|
+
sigma: float = 10
|
|
58
|
+
threshold: int = 1
|
|
59
|
+
window_size: int | None = None
|
|
60
|
+
merge_length: int | None = None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class Parameters(BaseData):
|
|
64
|
+
"""
|
|
65
|
+
Edge detection input parameters.
|
|
66
|
+
|
|
67
|
+
:param detection: Detection parameters expected for the edge detection.
|
|
68
|
+
:param source: Parameters for the source object and data.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
model_config = ConfigDict(arbitrary_types_allowed=True)
|
|
72
|
+
|
|
73
|
+
_name: str = NAME
|
|
74
|
+
|
|
75
|
+
input_file: InputFile | None = InputFile.read_ui_json(
|
|
76
|
+
DEFAULT_UI_JSON, validate=False
|
|
77
|
+
)
|
|
78
|
+
detection: DetectionParameters
|
|
79
|
+
output: OutputParameters
|
|
80
|
+
run_command: str = f"curve_apps.{NAME}.driver"
|
|
81
|
+
source: SourceParameters
|
|
82
|
+
title: str = NAME.capitalize().replace("_", " ")
|
curve_apps/params.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Copyright (c) 2024 Mira Geoscience Ltd.
|
|
2
|
+
#
|
|
3
|
+
# This file is part of edge-detection package.
|
|
4
|
+
#
|
|
5
|
+
# All rights reserved.
|
|
6
|
+
#
|
|
7
|
+
#
|
|
8
|
+
# This file is part of curve-apps.
|
|
9
|
+
#
|
|
10
|
+
# curve-apps is distributed under the terms and conditions of the MIT License
|
|
11
|
+
# (see LICENSE file at the root of this source code package).
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from pydantic import BaseModel
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class OutputParameters(BaseModel):
|
|
19
|
+
"""
|
|
20
|
+
Output parameters expected by the ui.json file format.
|
|
21
|
+
|
|
22
|
+
:param export_as: Name of the output entity.
|
|
23
|
+
:param out_group: Name of the output group.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
export_as: str | None = None
|
|
27
|
+
out_group: str | None = None
|