Skip to content

Commit 359209c

Browse files
committed
doc: adding more docstrings and type hints
1 parent be804c7 commit 359209c

File tree

5 files changed

+124
-58
lines changed

5 files changed

+124
-58
lines changed

LoopStructural/modelling/features/_base_geological_feature.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,24 @@ class BaseFeature:
1212
Base class for geological features.
1313
"""
1414

15-
def __init__(self, name, model=None, faults=[], regions=[], builder=None):
15+
def __init__(self, name : str, model=None, faults: list=[], regions: list=[], builder=None):
16+
"""Base geological feature, this is a virtucal class and should not be
17+
used directly. Inheret from this to implement a new type of geological
18+
feature or use one of the exisitng implementations
19+
20+
Parameters
21+
----------
22+
name :
23+
Name of the geological feature to add
24+
model : GeologicalModel, optional
25+
the model the feature is associated with, by default None
26+
faults : list, optional
27+
any faults that fault this feature, by default []
28+
regions : list, optional
29+
any regions that affect this feature, by default []
30+
builder : GeologicalFeatureBuilder, optional
31+
the builder of the feature, by default None
32+
"""
1633
self.name = name
1734
self.type = FeatureType.BASE
1835
self.regions = regions
@@ -44,9 +61,12 @@ def model(self):
4461

4562
@model.setter
4663
def model(self, model):
64+
from LoopStructural import GeologicalModel
4765
# causes circular import, could delay import?
48-
# if type(model) == GeologicalModel:
49-
self._model = model
66+
if type(model) == GeologicalModel:
67+
self._model = model
68+
else:
69+
raise TypeError("Model must be a GeologicalModel")
5070

5171
def toggle_faults(self):
5272
"""
@@ -79,6 +99,18 @@ def add_region(self, region):
7999
self.regions.append(region)
80100

81101
def __call__(self, xyz):
102+
""" Calls evaluate_value method
103+
104+
Parameters
105+
----------
106+
xyz : np.ndarray
107+
location to evaluate feature
108+
109+
Returns
110+
-------
111+
np.ndarray
112+
the value of the feature at the locations
113+
"""
82114
return self.evaluate_value(xyz)
83115

84116
def evaluate_value(self, pos):
@@ -94,11 +126,27 @@ def evaluate_gradient(self, pos):
94126
raise NotImplementedError
95127

96128
def min(self):
129+
"""Calculate the min value of the geological feature
130+
in the model
131+
132+
Returns
133+
-------
134+
minimum, float
135+
min value of the feature evaluated on a regular grid in the model domain
136+
"""
97137
if self.model is None:
98138
return 0
99139
return np.nanmin(self.evaluate_value(self.model.regular_grid((10, 10, 10))))
100140

101141
def max(self):
142+
"""Calculate the maximum value of the geological feature
143+
in the model
144+
145+
Returns
146+
-------
147+
maximum, float
148+
max value of the feature evaluated on a regular grid in the model domain
149+
"""
102150
if self.model is None:
103151
return 0
104152
return np.nanmax(self.evaluate_value(self.model.regular_grid((10, 10, 10))))

LoopStructural/modelling/features/_cross_product_geological_feature.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313

1414
class CrossProductGeologicalFeature(BaseFeature):
15-
def __init__(self, name, geological_feature_a, geological_feature_b):
15+
def __init__(self, name : str, geological_feature_a : BaseFeature, geological_feature_b: BaseFeature):
1616
"""
17+
1718
Create a geological feature for a vector field using the cross
1819
product between
1920
two existing features
@@ -22,18 +23,29 @@ def __init__(self, name, geological_feature_a, geological_feature_b):
2223
name: feature name
2324
geological_feature_a: first feature
2425
geological_feature_b: second feature
26+
27+
28+
Parameters
29+
----------
30+
name : str
31+
name of the feature
32+
geological_feature_a : BaseFeature
33+
Left hand side of cross product
34+
geological_feature_b : BaseFeature
35+
Right hand side of cross product
2536
"""
2637
super().__init__(name)
2738
self.geological_feature_a = geological_feature_a
2839
self.geological_feature_b = geological_feature_b
2940
self.value_feature = None
3041

31-
def evaluate_gradient(self, locations):
42+
def evaluate_gradient(self, locations : np.ndarray) ->np.ndarray:
3243
"""
3344
Calculate the gradient of the geological feature by using numpy to
3445
calculate the cross
3546
product between the two existing feature gradients.
3647
This means both features have to be evaluated for the locations
48+
3749
Parameters
3850
----------
3951
locations
@@ -48,7 +60,7 @@ def evaluate_gradient(self, locations):
4860
# v2 /= np.linalg.norm(v2,axis=1)[:,None]
4961
return np.cross(v1, v2, axisa=1, axisb=1)
5062

51-
def evaluate_value(self, evaluation_points):
63+
def evaluate_value(self, evaluation_points : np.ndarray) -> np.ndarray:
5264
"""
5365
Return 0 because there is no value for this feature
5466
Parameters

LoopStructural/modelling/features/_geological_feature.py

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from LoopStructural.modelling.features import BaseFeature
55
from LoopStructural.utils import getLogger
66
from LoopStructural.modelling.features import FeatureType
7+
from LoopStructural.interpolators import GeologicalInterpolator
78
import numpy as np
89

910
from LoopStructural.utils import getLogger, LoopValueError
@@ -34,24 +35,23 @@ class GeologicalFeature(BaseFeature):
3435

3536
def __init__(
3637
self,
37-
name,
38-
interpolator,
38+
name : str,
39+
interpolator : GeologicalInterpolator,
3940
builder=None,
40-
regions=[],
41-
faults=[],
41+
regions : list=[],
42+
faults : list =[],
4243
model=None,
4344
):
4445
"""Default constructor for geological feature
4546
4647
Parameters
4748
----------
48-
name: string
49+
name: str
4950
interpolator : GeologicalInterpolator
5051
builder : GeologicalFeatureBuilder
51-
data :
52-
region :
53-
type :
52+
region : list
5453
faults : list
54+
model : GeologicalModel
5555
5656
5757
"""
@@ -73,14 +73,14 @@ def __setitem__(self, key, item):
7373
def set_model(self, model):
7474
self.model = model
7575

76-
def evaluate_value(self, evaluation_points):
76+
def evaluate_value(self, evaluation_points : np.ndarray) -> np.ndarray:
7777
"""
7878
Evaluate the scalar field value of the geological feature at the locations
7979
specified
8080
8181
Parameters
8282
----------
83-
evaluation_points : numpy array
83+
evaluation_points : np.ndarray
8484
location to evaluate the scalar value
8585
8686
Returns
@@ -116,7 +116,7 @@ def evaluate_value(self, evaluation_points):
116116
v[mask] = self.interpolator.evaluate_value(evaluation_points[mask, :])
117117
return v
118118

119-
def evaluate_gradient(self, evaluation_points):
119+
def evaluate_gradient(self, evaluation_points : np.ndarray) -> np.ndarray:
120120
"""
121121
122122
Parameters
@@ -127,6 +127,7 @@ def evaluate_gradient(self, evaluation_points):
127127
Returns
128128
-------
129129
130+
130131
"""
131132
if evaluation_points.shape[1] != 3:
132133
raise LoopValueError("Need Nx3 array of xyz points to evaluate gradient")
@@ -192,41 +193,3 @@ def evaluate_value_misfit(self):
192193
diff /= self.max() - self.min()
193194
return diff
194195

195-
def mean(self):
196-
"""
197-
Calculate average of the support values
198-
199-
Returns
200-
-------
201-
mean : double
202-
average value of the feature evaluated on a (10,10,10) grid in model area
203-
204-
"""
205-
if self.model is None:
206-
return 0
207-
return np.mean(self.evaluate_value(self.model.regular_grid((10, 10, 10))))
208-
209-
def min(self):
210-
"""
211-
212-
Returns
213-
-------
214-
min : double
215-
min value of the feature evaluated on a (10,10,10) grid in model area
216-
"""
217-
if self.model is None:
218-
return 0
219-
return np.nanmin(self.evaluate_value(self.model.regular_grid((10, 10, 10))))
220-
221-
def max(self):
222-
"""
223-
Calculate average of the support values
224-
225-
Returns
226-
-------
227-
max : double
228-
max value of the feature evaluated on a (10,10,10) grid in model area
229-
"""
230-
if self.model is None:
231-
return 0
232-
return np.nanmax(self.evaluate_value(self.model.regular_grid((10, 10, 10))))

LoopStructural/modelling/features/_lambda_geological_feature.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,63 @@ def __init__(
2020
faults=[],
2121
builder=None,
2222
):
23+
"""_summary_
24+
25+
Parameters
26+
----------
27+
function : _type_, optional
28+
_description_, by default None
29+
name : str, optional
30+
_description_, by default "unnamed_lambda"
31+
gradient_function : _type_, optional
32+
_description_, by default None
33+
model : _type_, optional
34+
_description_, by default None
35+
regions : list, optional
36+
_description_, by default []
37+
faults : list, optional
38+
_description_, by default []
39+
builder : _type_, optional
40+
_description_, by default None
41+
"""
2342
BaseFeature.__init__(self, name, model, faults, regions, builder)
2443
self.type = FeatureType.LAMBDA
2544
self.function = function
2645
self.gradient_function = gradient_function
2746

28-
def evaluate_value(self, xyz):
47+
def evaluate_value(self, xyz : np.ndarray) -> np.ndarray:
48+
"""_summary_
49+
50+
Parameters
51+
----------
52+
xyz : np.ndarray
53+
_description_
54+
55+
Returns
56+
-------
57+
np.ndarray
58+
_description_
59+
"""
2960
v = np.zeros((xyz.shape[0]))
3061
if self.function is None:
3162
v[:] = np.nan
3263
else:
3364
v[:] = self.function(xyz)
3465
return v
3566

36-
def evaluate_gradient(self, xyz):
67+
def evaluate_gradient(self, xyz : np.ndarray) -> np.ndarray:
68+
"""_summary_
69+
70+
Parameters
71+
----------
72+
xyz : np.ndarray
73+
_description_
74+
75+
Returns
76+
-------
77+
np.ndarray
78+
_description_
79+
"""
3780
v = np.zeros((xyz.shape[0], 3))
3881
if self.gradient_function is None:
3982
v[:, :] = np.nan

LoopStructural/modelling/features/_structural_frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class StructuralFrame(BaseFeature):
14-
def __init__(self, name, features, fold=None):
14+
def __init__(self, name : str, features: list, fold=None):
1515
"""
1616
Structural frame is a curvilinear coordinate system defined by
1717
structural observations associated with a fault or fold.

0 commit comments

Comments
 (0)