|
| 1 | +""" setters API |
| 2 | +
|
| 3 | +""" |
| 4 | +from gempy import get_data |
| 5 | +from gempy_plugins.utils import _setdoc, _setdoc_pro |
| 6 | +from gempy_plugins.utils import docstring as ds |
| 7 | +from gempy.core.model import Model, InterpolatorModel |
| 8 | +import warnings |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +# This warning comes from numpy complaining about a aesara optimization |
| 12 | +warnings.filterwarnings("ignore", |
| 13 | + message='.* a non-tuple sequence for multidimensional ' |
| 14 | + 'indexing is deprecated; use*.', |
| 15 | + append=True) |
| 16 | + |
| 17 | + |
| 18 | +@_setdoc([InterpolatorModel.__doc__]) |
| 19 | +@_setdoc_pro([Model.__doc__, ds.compile_aesara, ds.aesara_optimizer]) |
| 20 | +def set_interpolator(geo_model: Model, output: list = None, compile_aesara: bool = True, |
| 21 | + aesara_optimizer=None, verbose: list = None, grid=None, type_=None, |
| 22 | + update_structure=True, update_kriging=True, |
| 23 | + **kwargs): |
| 24 | + """ |
| 25 | + Method to create a graph and compile the aesara code to compute the interpolation. |
| 26 | +
|
| 27 | + Args: |
| 28 | + geo_model (:class:`gempy.core.model.Project`): [s0] |
| 29 | + output (list[str:{geo, grav}]): type of interpolation. |
| 30 | + compile_aesara (bool): [s1] |
| 31 | + aesara_optimizer (str {'fast_run', 'fast_compile'}): [s2] |
| 32 | + verbose: |
| 33 | + update_kriging (bool): reset kriging values to its default. |
| 34 | + update_structure (bool): sync Structure instance before setting aesara graph. |
| 35 | +
|
| 36 | + Keyword Args: |
| 37 | + - pos_density (Optional[int]): Only necessary when type='grav'. Location on the Surfaces().df |
| 38 | + where density is located (starting on id being 0). |
| 39 | + - Vs |
| 40 | + - pos_magnetics |
| 41 | +
|
| 42 | + Returns: |
| 43 | +
|
| 44 | + """ |
| 45 | + # output = list(output) |
| 46 | + if output is None: |
| 47 | + output = ['geology'] |
| 48 | + |
| 49 | + if type(output) is not list: |
| 50 | + raise TypeError('Output must be a list.') |
| 51 | + |
| 52 | + # TODO Geology is necessary for everthing? |
| 53 | + if 'gravity' in output and 'geology' not in output: |
| 54 | + output.append('geology') |
| 55 | + |
| 56 | + if 'magnetics' in output and 'geology' not in output: |
| 57 | + output.append('geology') |
| 58 | + |
| 59 | + if type_ is not None: |
| 60 | + warnings.warn('type warn is going to be deprecated. Use output insted', FutureWarning) |
| 61 | + output = type_ |
| 62 | + |
| 63 | + if aesara_optimizer is not None: |
| 64 | + geo_model._additional_data.options.df.at['values', 'aesara_optimizer'] = aesara_optimizer |
| 65 | + if verbose is not None: |
| 66 | + geo_model._additional_data.options.df.at['values', 'verbosity'] = verbose |
| 67 | + if 'dtype' in kwargs: |
| 68 | + geo_model._additional_data.options.df.at['values', 'dtype'] = kwargs['dtype'] |
| 69 | + if 'device' in kwargs: |
| 70 | + geo_model._additional_data.options.df.at['values', 'device'] = kwargs['device'] |
| 71 | + |
| 72 | + # TODO add kwargs |
| 73 | + geo_model._rescaling.rescale_data() |
| 74 | + geo_model.update_additional_data(update_structure=update_structure, update_kriging=update_kriging) |
| 75 | + geo_model.update_to_interpolator() |
| 76 | + geo_model._surface_points.sort_table() |
| 77 | + geo_model._orientations.sort_table() |
| 78 | + |
| 79 | + geo_model._interpolator.create_aesara_graph(geo_model._additional_data, inplace=True, |
| 80 | + output=output, **kwargs) |
| 81 | + |
| 82 | + if 'gravity' in output: |
| 83 | + pos_density = kwargs.get('pos_density', 1) |
| 84 | + tz = kwargs.get('tz', 'auto') |
| 85 | + if geo_model._grid.centered_grid is not None: |
| 86 | + geo_model._interpolator.set_aesara_shared_gravity(tz, pos_density) |
| 87 | + |
| 88 | + if 'magnetics' in output: |
| 89 | + pos_magnetics = kwargs.get('pos_magnetics', 1) |
| 90 | + Vs = kwargs.get('Vs', 'auto') |
| 91 | + incl = kwargs.get('incl') |
| 92 | + decl = kwargs.get('decl') |
| 93 | + B_ext = kwargs.get('B_ext', 52819.8506939139e-9) |
| 94 | + if geo_model._grid.centered_grid is not None: |
| 95 | + geo_model._interpolator.set_aesara_shared_magnetics(Vs, pos_magnetics, incl, decl, B_ext) |
| 96 | + |
| 97 | + if 'topology' in output: |
| 98 | + # This id is necessary for topology |
| 99 | + id_list = geo_model._surfaces.df.groupby('isFault').cumcount() + 1 |
| 100 | + geo_model.add_surface_values(id_list, 'topology_id') |
| 101 | + geo_model._interpolator.set_aesara_shared_topology() |
| 102 | + |
| 103 | + # TODO it is missing to pass to aesara the position of topology_id |
| 104 | + |
| 105 | + if compile_aesara is True: |
| 106 | + geo_model._interpolator.set_all_shared_parameters(reset_ctrl=True) |
| 107 | + |
| 108 | + geo_model._interpolator.compile_th_fn_geo(inplace=True, grid=grid) |
| 109 | + else: |
| 110 | + if grid == 'shared': |
| 111 | + geo_model._interpolator.set_aesara_shared_grid(grid) |
| 112 | + |
| 113 | + print('Kriging values: \n', geo_model._additional_data.kriging_data) |
| 114 | + return geo_model._interpolator |
| 115 | + |
| 116 | + |
| 117 | +@_setdoc_pro([Model.__doc__]) |
| 118 | +def set_geometric_data(geo_model: Model, surface_points_df=None, |
| 119 | + orientations_df=None, **kwargs): |
| 120 | + """ Function to set directly pandas.Dataframes to the gempy geometric data objects |
| 121 | +
|
| 122 | + Args: |
| 123 | + geo_model: [s0] |
| 124 | + surface_points_df: A pn.Dataframe object with X, Y, Z, and surface columns |
| 125 | + orientations_df: A pn.Dataframe object with X, Y, Z, surface columns and pole or orientation columns |
| 126 | + **kwargs: |
| 127 | +
|
| 128 | + Returns: |
| 129 | + Modified df |
| 130 | + """ |
| 131 | + |
| 132 | + r_ = None |
| 133 | + |
| 134 | + if surface_points_df is not None: |
| 135 | + geo_model.set_surface_points(surface_points_df, **kwargs) |
| 136 | + r_ = 'surface_points' |
| 137 | + |
| 138 | + elif orientations_df is not None: |
| 139 | + geo_model.set_orientations(orientations_df, **kwargs) |
| 140 | + r_ = 'data' if r_ == 'surface_points' else 'orientations' |
| 141 | + |
| 142 | + else: |
| 143 | + raise AttributeError('You need to pass at least one dataframe') |
| 144 | + |
| 145 | + return get_data(geo_model, itype=r_) |
| 146 | + |
| 147 | + |
| 148 | +def set_orientation_from_surface_points(geo_model, indices_array): |
| 149 | + """ |
| 150 | + Create and set orientations from at least 3 points of the :attr:`gempy.data_management.InputData.surface_points` |
| 151 | + Dataframe |
| 152 | +
|
| 153 | + Args: |
| 154 | + geo_model (:class:`Model`): |
| 155 | + indices_array (array-like): 1D or 2D array with the pandas indices of the |
| 156 | + :attr:`surface_points`. If 2D every row of the 2D matrix will be used to create an |
| 157 | + orientation |
| 158 | +
|
| 159 | +
|
| 160 | + Returns: |
| 161 | + :attr:`orientations`: Already updated inplace |
| 162 | + """ |
| 163 | + |
| 164 | + if np.ndim(indices_array) == 1: |
| 165 | + indices = indices_array |
| 166 | + form = geo_model._surface_points.df['surface'].loc[indices].unique() |
| 167 | + assert form.shape[0] == 1, 'The interface points must belong to the same surface' |
| 168 | + form = form[0] |
| 169 | + |
| 170 | + ori_parameters = geo_model._orientations.create_orientation_from_surface_points(geo_model._surface_points, indices) |
| 171 | + geo_model.add_orientations( |
| 172 | + X=ori_parameters[0], |
| 173 | + Y=ori_parameters[1], |
| 174 | + Z=ori_parameters[2], |
| 175 | + orientation=ori_parameters[3:6], |
| 176 | + pole_vector=ori_parameters[6:9], |
| 177 | + surface=form |
| 178 | + ) |
| 179 | + elif np.ndim(indices_array) == 2: |
| 180 | + for indices in indices_array: |
| 181 | + form = geo_model._surface_points.df['surface'].loc[indices].unique() |
| 182 | + assert form.shape[0] == 1, 'The interface points must belong to the same surface' |
| 183 | + form = form[0] |
| 184 | + ori_parameters = geo_model._orientations.create_orientation_from_surface_points( |
| 185 | + geo_model._surface_points, indices) |
| 186 | + geo_model.add_orientations(X=ori_parameters[0], Y=ori_parameters[1], Z=ori_parameters[2], |
| 187 | + orientation=ori_parameters[3:6], pole_vector=ori_parameters[6:9], |
| 188 | + surface=form) |
| 189 | + |
| 190 | + return geo_model._orientations |
| 191 | + |
| 192 | + |
| 193 | +def select_nearest_surfaces_points(geo_model, surface_points, searchcrit): |
| 194 | + """ |
| 195 | + Find the neighbour points of the same surface |
| 196 | + by given radius (radius-search) or fix number (knn). |
| 197 | + Parameters |
| 198 | + ---------- |
| 199 | + geo_model : geo_model |
| 200 | + GemPy-model. |
| 201 | + surface_points: Pandas-dataframe |
| 202 | + Contains the dataframe of the (point-)data from the GemPy-model. |
| 203 | + searchcrit : int or float |
| 204 | + if is int: uses knn-search. |
| 205 | + if is float: uses radius-search. |
| 206 | + """ |
| 207 | + |
| 208 | + from sklearn.neighbors import NearestNeighbors |
| 209 | + |
| 210 | + # extract surface names |
| 211 | + surfaces = np.unique(surface_points['surface']) |
| 212 | + neighbours = [] |
| 213 | + # for each surface |
| 214 | + if isinstance(searchcrit, int): # in case knn-search |
| 215 | + searchcrit = searchcrit + 1 # because the point itself is also found |
| 216 | + for s in range(surfaces.size): |
| 217 | + # extract point-ids |
| 218 | + i_surfaces = surface_points['surface'] == surfaces[s] |
| 219 | + # extract point coordinates |
| 220 | + p_surfaces = surface_points[i_surfaces][['X', 'Y', 'Z']] |
| 221 | + # create search-tree |
| 222 | + Tree = NearestNeighbors(n_neighbors=searchcrit) |
| 223 | + # add data to tree |
| 224 | + Tree.fit(p_surfaces) |
| 225 | + # find neighbours |
| 226 | + neighbours_surfaces = Tree.kneighbors(p_surfaces, n_neighbors=searchcrit, |
| 227 | + return_distance=False) |
| 228 | + # add neighbours with initial index to total list |
| 229 | + for n in neighbours_surfaces: |
| 230 | + neighbours.append(p_surfaces.index[n]) |
| 231 | + else: # in case radius-search |
| 232 | + for s in range(surfaces.size): |
| 233 | + # extract point-ids |
| 234 | + i_surfaces = surface_points['surface'] == surfaces[s] |
| 235 | + # extract point coordinates |
| 236 | + p_surfaces = surface_points[i_surfaces][['X', 'Y', 'Z']] |
| 237 | + # create search-tree |
| 238 | + Tree = NearestNeighbors(radius=searchcrit) |
| 239 | + # add data to tree |
| 240 | + Tree.fit(p_surfaces) |
| 241 | + # find neighbours (attention: relativ index!) |
| 242 | + neighbours_surfaces = Tree.radius_neighbors(p_surfaces, |
| 243 | + radius=searchcrit, |
| 244 | + return_distance=False) |
| 245 | + # add neighbours with initial index to total list |
| 246 | + for n in neighbours_surfaces: |
| 247 | + neighbours.append(p_surfaces.index[n]) |
| 248 | + return neighbours |
| 249 | + |
| 250 | + |
| 251 | +def set_orientation_from_neighbours(geo_model, neighbours): |
| 252 | + """ |
| 253 | + Calculates the orientation of one point with its neighbour points |
| 254 | + of the same surface. |
| 255 | + Parameters |
| 256 | + ---------- |
| 257 | + geo_model : geo_model |
| 258 | + GemPy-model. |
| 259 | + neighbours : Int64Index |
| 260 | + point-neighbours-id, first id is the point itself. |
| 261 | + """ |
| 262 | + |
| 263 | + from sklearn.preprocessing import normalize |
| 264 | + |
| 265 | + # compute normal vector for the point |
| 266 | + if neighbours.size > 2: |
| 267 | + # extract point coordinates |
| 268 | + coo = geo_model._surface_points.df.loc[neighbours][['X', 'Y', 'Z']] |
| 269 | + # calculates covariance matrix |
| 270 | + cov = np.cov(coo.T) |
| 271 | + # calculate normalized normal vector |
| 272 | + normvec = normalize(np.cross(cov[0].T, cov[1].T).reshape(1, -1))[0] |
| 273 | + # check orientation of normal vector (has to be oriented to sky) |
| 274 | + if normvec[2] < 0: |
| 275 | + normvec = normvec * (-1) |
| 276 | + # append to the GemPy-model |
| 277 | + geo_model.add_orientations(geo_model._surface_points.df['X'][neighbours[0]], |
| 278 | + geo_model._surface_points.df['Y'][neighbours[0]], |
| 279 | + geo_model._surface_points.df['Z'][neighbours[0]], |
| 280 | + geo_model._surface_points.df['surface'][neighbours[0]], |
| 281 | + normvec.tolist()) |
| 282 | + # if computation is impossible set normal vector to default orientation |
| 283 | + else: |
| 284 | + print("orientation calculation of point" + str(neighbours[0]) + "is impossible") |
| 285 | + print("-> default vector is set [0,0,1]") |
| 286 | + geo_model.add_orientations(geo_model._surface_points.df['X'][neighbours[0]], |
| 287 | + geo_model._surface_points.df['Y'][neighbours[0]], |
| 288 | + geo_model._surface_points.df['Z'][neighbours[0]], |
| 289 | + geo_model._surface_points.df['surface'][neighbours[0]], |
| 290 | + orientation=[0, 0, 1]) |
| 291 | + return geo_model._orientations |
| 292 | + |
| 293 | + |
| 294 | +def set_orientation_from_neighbours_all(geo_model, neighbours): |
| 295 | + """ |
| 296 | + Calculates the orientations for all points with given neighbours. |
| 297 | + Parameters |
| 298 | + ---------- |
| 299 | + geo_model : geo_model |
| 300 | + GemPy-model. |
| 301 | + neighbours : list of Int64Index |
| 302 | + point-neighbours-IDs, the first id is the id of the point |
| 303 | + for which the orientation is calculated. |
| 304 | + """ |
| 305 | + |
| 306 | + # compute normal vector for the points |
| 307 | + for n in neighbours: |
| 308 | + set_orientation_from_neighbours(geo_model, n) |
| 309 | + |
| 310 | + return geo_model._orientations |
0 commit comments