geost.base.Collection.segmentize_by_gradient#

Collection.segmentize_by_gradient(column: str, threshold: float, output_column: str | None = None, use_mid_depth: bool = True, inplace: bool = False) → Collection | None[source]#

Segmentize the data based on gradient changes in a column of the data table. A segment is made up of consecutive rows where the gradient of column is below a specified threshold and a new segment begins when the threshold is exceeded. Each segment gets a unique number.

Parameters:
  • column (str) – Column to determine the segments from.

  • threshold (float) – Minimum gradient change to create a new segment.

  • output_column (str | None, optional) – Name of the column to store segment numbers. The default is None, then the name will that of the column with a “_segment” suffix.

  • use_mid_depth (bool, optional) – Whether to use mid-depth for gradient. The default is True.

  • inplace (bool, optional) – If True, add the result to the data table of the current Collection. In this case, the method returns None. Otherwise, return a new Collection instance with the result added in the data table. The default is False.

Returns:

New Collection with the gradient added to the data table, or None if inplace=True.

Return type:

Collection or None

Raises:
  • KeyError – If the column is not found in the data.

  • TypeError – If column is not of a numeric dtype.

Notes

This method operates on the data table of the Collection and assumes that the rows are sorted by survey and ascending depth.

Examples

Suppose we have a Collection with a “resistance” in MPa for each layer in the data table. We want to create segments of consecutive rows where the resistance does not change more than 0.5 MPa per meters. First, we calculate the gradient of the “resistance” column so we can use this to inspect the result of segmentize_by_gradient:

>>> import geost
>>> collection = geost.Collection(
...     {
...         "nr": ["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"],
...         "surface": [0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3],
...         "top": [0.0, 0.8, 1.5, 2.5, 3.7, 0.0, 0.6, 1.2, 2.5, 3.1],
...         "bottom": [0.8, 1.5, 2.5, 3.7, 4.2, 0.6, 1.2, 2.5, 3.1, 3.9],
...         "resistance": [0.25, 0.95, 0.19, 0.18, 0.35, 0.23, 0.67, 0.12, 0.9, 0.86],
...     }
... )
>>> collection = collection.calculate_gradient("resistance")
>>> collection.data
  nr  surface  top  bottom  resistance  gradient
0  A      0.2  0.0     0.8        0.25  0.000000
1  A      0.2  0.8     1.5        0.95  0.933333
2  A      0.2  1.5     2.5        0.19 -0.894118
3  A      0.2  2.5     3.7        0.18 -0.009091
4  A      0.2  3.7     4.2        0.35  0.200000
5  B      0.3  0.0     0.6        0.23  0.000000
6  B      0.3  0.6     1.2        0.67  0.733333
7  B      0.3  1.2     2.5        0.12 -0.578947
8  B      0.3  2.5     3.1        0.90  0.821053
9  B      0.3  3.1     3.9        0.86 -0.057143

We can create the segment column with the desired threshold of 0.5:

>>> collection = collection.segmentize_by_gradient("resistance", 0.5)
>>> collection.data
  nr  surface  top  bottom  resistance  gradient  resistance_segment
0  A      0.2  0.0     0.8        0.25  0.000000                   1
1  A      0.2  0.8     1.5        0.95  0.933333                   2
2  A      0.2  1.5     2.5        0.19 -0.894118                   3
3  A      0.2  2.5     3.7        0.18 -0.009091                   3
4  A      0.2  3.7     4.2        0.35  0.200000                   3
5  B      0.3  0.0     0.6        0.23  0.000000                   4
6  B      0.3  0.6     1.2        0.67  0.733333                   5
7  B      0.3  1.2     2.5        0.12 -0.578947                   6
8  B      0.3  2.5     3.1        0.90  0.821053                   7
9  B      0.3  3.1     3.9        0.86 -0.057143                   7

Using use_mid_depth=False changes the result. Also, we can modify the output column name and modify the existing Collection without creating a new one:

>>> collection.segmentize_by_gradient(
...     "resistance", 0.5, use_mid_depth=False, output_column="segment", inplace=True
... )
>>> collection.data
  nr  surface  top  bottom  resistance  gradient  segment
0  A      0.2  0.0     0.8        0.25  0.000000        1
1  A      0.2  0.8     1.5        0.95  0.933333        2
2  A      0.2  1.5     2.5        0.19 -0.894118        3
3  A      0.2  2.5     3.7        0.18 -0.009091        3
4  A      0.2  3.7     4.2        0.35  0.200000        3
5  B      0.3  0.0     0.6        0.23  0.000000        4
6  B      0.3  0.6     1.2        0.67  0.733333        5
7  B      0.3  1.2     2.5        0.12 -0.578947        5
8  B      0.3  2.5     3.1        0.90  0.821053        6
9  B      0.3  3.1     3.9        0.86 -0.057143        6