Interactive modeling for gravity and magnetics

This notebook will show you how to use the interactivity functions of fatiando.gravmag.interactive.

Some of them don't play nicely with IPython notebooks because they use the matplotlib plot window as a GUI. In such cases, you won't be able to use %matplotlib inline. However, the GUIs usually provide some method of embedding a plot of the resulting model in the notebook.

Other functions use the IPython HTML widgets for interactivity. They will make sure that the widgets work even when %matplotlib inline is not activated.

License

All content can be freely used and adapted under the terms of the Creative Commons Attribution 4.0 International License.


In [1]:
from fatiando.gravmag.interactive import Moulder
from fatiando.vis import mpl
import numpy as np

Moulder for 2D modeling with polygons

The Moulder class is a matplotlib GUI application for 2D modeling using polygons. It allows interactive drawing, editing, moving, deleting, of polygons.

First thing to do is create the application object. We'll need to specify the limits of the model area (min and max in x and z) and the points where the predicted data will be calculated (x and z arrays).


In [2]:
area = (0, 10e3, 0, 5e3)
x = np.linspace(area[0], area[1], 100)
z = np.zeros_like(x)

In [3]:
app = Moulder(area, x, z)

To start the GUI and draw a model, call:


In [8]:
app.run()

If everything went well, you should see a window like this pop-up:

To draw a new polygon, press n and start clicking on the lower panel. To close the polygon and finish, press the right mouse button. To set the density of the polygon, click on it to select (the outline should become green) and move the density slider on the bottom. You can edit the polygon by dragging it or its vertices. See the intructions in the figure title for other options.

Let's insert an image of the model on the IPython notebook for the good people of the internet to see:


In [9]:
app.plot()


Out[9]:

We can get the generated model and data from the Moulder object to use in other places:


In [10]:
app.model


Out[10]:
[<fatiando.mesher.Polygon at 0x7f19590a2510>,
 <fatiando.mesher.Polygon at 0x7f1958fb6450>]

In [12]:
app.error


Out[12]:
0.18229166666666607

In [13]:
app.predicted


Out[13]:
array([ -3.00836191e+00,  -3.07533725e+00,  -3.63436057e+00,
        -3.60987335e+00,  -3.94022057e+00,  -3.78291722e+00,
        -4.24248705e+00,  -4.25944008e+00,  -4.45484039e+00,
        -4.35782110e+00,  -5.14041742e+00,  -5.37193296e+00,
        -5.40614242e+00,  -5.56066520e+00,  -5.83916522e+00,
        -6.37150648e+00,  -6.67144359e+00,  -6.47487795e+00,
        -6.99662379e+00,  -7.08237367e+00,  -7.02393148e+00,
        -7.13635972e+00,  -7.16851846e+00,  -7.53378080e+00,
        -7.09541499e+00,  -7.16382335e+00,  -6.95094571e+00,
        -6.62817455e+00,  -6.50091572e+00,  -6.09133226e+00,
        -5.53868149e+00,  -5.15402480e+00,  -4.94661593e+00,
        -4.64071911e+00,  -4.47151531e+00,  -3.87451729e+00,
        -3.53180212e+00,  -3.04339209e+00,  -2.73771963e+00,
        -2.80028132e+00,  -2.30094608e+00,  -2.37308529e+00,
        -1.64557794e+00,  -1.54417211e+00,  -1.66837371e+00,
        -1.44057157e+00,  -8.95539698e-01,  -9.43335371e-01,
        -2.37971419e-01,  -3.14932907e-01,  -8.51905182e-04,
         4.87201201e-02,   5.15749043e-01,   6.64162618e-01,
         1.08341102e+00,   1.23903715e+00,   1.55246228e+00,
         1.68051314e+00,   2.20802350e+00,   2.56003990e+00,
         3.08206970e+00,   3.73020270e+00,   3.99088680e+00,
         4.38956076e+00,   5.41605808e+00,   6.37717626e+00,
         7.26529908e+00,   8.25610954e+00,   9.02488178e+00,
         8.21007919e+00,   7.10558780e+00,   6.33406145e+00,
         5.96489257e+00,   4.76732051e+00,   3.96996536e+00,
         3.82073976e+00,   3.41802323e+00,   3.38814371e+00,
         2.59863179e+00,   2.70066036e+00,   2.21327445e+00,
         2.09724009e+00,   1.98097046e+00,   1.86202942e+00,
         1.86606414e+00,   1.86082344e+00,   1.30543330e+00,
         1.36546762e+00,   1.34035549e+00,   1.34899120e+00,
         1.33429472e+00,   1.08845481e+00,   9.26616589e-01,
         1.07391295e+00,   9.59774248e-01,   7.21172615e-01,
         1.03604059e+00,   6.63013009e-01,   5.55077253e-01,
         6.81256173e-01])

The save_predicted method saves the predicted data to a text file in space separated columns: x y data.


In [25]:
app.save_predicted('model-data.txt')

In [26]:
!head model-data.txt


0.000000000000000000e+00 0.000000000000000000e+00 -3.008361910097954617e+00
1.010101010101010104e+02 0.000000000000000000e+00 -3.075337252588699499e+00
2.020202020202020208e+02 0.000000000000000000e+00 -3.634360571059106704e+00
3.030303030303030027e+02 0.000000000000000000e+00 -3.609873351984154155e+00
4.040404040404040416e+02 0.000000000000000000e+00 -3.940220569325843414e+00
5.050505050505050804e+02 0.000000000000000000e+00 -3.782917221375249106e+00
6.060606060606060055e+02 0.000000000000000000e+00 -4.242487047110039988e+00
7.070707070707070443e+02 0.000000000000000000e+00 -4.259440076413939735e+00
8.080808080808080831e+02 0.000000000000000000e+00 -4.454840392729096266e+00
9.090909090909091219e+02 0.000000000000000000e+00 -4.357821101217265181e+00

We can also save the entire application to a pickle file. This allows us to restore the application later on in another Python/IPython session.


In [27]:
app.save('moulder-app.pkl')

So if we delete the app, we can still restore it and continue modeling like nothing hapenned.


In [28]:
del app

In [31]:
reloaded = Moulder.load('moulder-app.pkl')
reloaded.plot()


Out[31]:

To continue modeling, just call reloaded.run(). For example, I can restart the app and add a new polygon:


In [32]:
reloaded.run()
reloaded.plot()


Out[32]: