From 5b4270560c3db885f99829467dbd1758be1ef167 Mon Sep 17 00:00:00 2001 From: rowanc1 Date: Mon, 25 Nov 2013 16:51:48 -0800 Subject: [PATCH] renamed video commands and added a more generic one. --- SimPEG/forward/Problem.py | 5 +- SimPEG/mesh/TensorView.py | 35 +- notebooks/SliceThroughModel.ipynb | 4059 +++++++++++++++++++++-------- 3 files changed, 3017 insertions(+), 1082 deletions(-) diff --git a/SimPEG/forward/Problem.py b/SimPEG/forward/Problem.py index 304f3912..54c623ee 100644 --- a/SimPEG/forward/Problem.py +++ b/SimPEG/forward/Problem.py @@ -226,7 +226,7 @@ class Problem(object): """ return sp.eye(m.size) - def createSyntheticData(self, m, std=0.05): + def createSyntheticData(self, m, std=0.05, u=None): """ Create synthetic data given a model, and a standard deviation. @@ -238,8 +238,7 @@ class Problem(object): Returns the observed data with random Gaussian noise and Wd which is the same size as data, and can be used to weight the inversion. """ - dobs = self.dpred(m) - dobs = dobs + dobs = self.dpred(m,u=u) noise = std*abs(dobs)*np.random.randn(*dobs.shape) dobs = dobs+noise eps = np.linalg.norm(mkvc(dobs),2)*1e-5 diff --git a/SimPEG/mesh/TensorView.py b/SimPEG/mesh/TensorView.py index 0374d1dd..4b1c4fc3 100644 --- a/SimPEG/mesh/TensorView.py +++ b/SimPEG/mesh/TensorView.py @@ -342,7 +342,7 @@ class TensorView(object): ax.set_zlabel('x3') if showIt: plt.show() - def Slicer(mesh, var, imageType='CC', normal='z', index=0, ax=None, clim=None): + def slicer(mesh, var, imageType='CC', normal='z', index=0, ax=None, clim=None): assert normal in 'xyz', 'normal must be x, y, or z' if ax is None: ax = plt.subplot(111) I = mesh.r(var,'CC','CC','M') @@ -357,17 +357,44 @@ class TensorView(object): ax.set_ylabel(axes[1]) return p - def SliceVideo(mesh,var,imageType='CC',normal='z',figsize=(10,8)): + def videoSlicer(mesh,var,imageType='CC',normal='z',figsize=(10,8)): + assert mesh.dim > 2, 'This is for 3D meshes only.' # First set up the figure, the axis, and the plot element we want to animate fig = plt.figure(figsize=figsize) ax = plt.axes() clim = [var.min(),var.max()] - plt.colorbar(mesh.Slicer(var, imageType=imageType, normal=normal, index=0, ax=ax, clim=clim)) + plt.colorbar(mesh.slicer(var, imageType=imageType, normal=normal, index=0, ax=ax, clim=clim)) tlt = plt.title(normal) def animateFrame(i): - mesh.Slicer(var, imageType=imageType, normal=normal, index=i, ax=ax, clim=clim) + mesh.slicer(var, imageType=imageType, normal=normal, index=i, ax=ax, clim=clim) tlt.set_text(normal.upper()+('-Slice: %d, %4.4f' % (i,getattr(mesh,'vectorCC'+normal)[i]))) return animate(fig, animateFrame, frames=mesh.nCv['xyz'.index(normal)]) + def video(mesh,var,function,figsize=(10,8)): + """ + Call a function for a list of models to create a video. + + :: + + def function(var, ax, clim, tlt, i): + tlt.set_text('%%d'%%i) + return mesh.plotImage(var, imageType='CC', ax=ax, clim=clim) + + mesh.video([model1, model2, ..., modeln],function) + """ + # First set up the figure, the axis, and the plot element we want to animate + fig = plt.figure(figsize=figsize) + ax = plt.axes() + VAR = np.concatenate(var) + clim = [VAR.min(),VAR.max()] + tlt = plt.title('') + plt.colorbar(function(var[0],ax,clim,tlt,0)) + + def animateFrame(i): + function(var[i],ax,clim,tlt,i) + + return animate(fig, animateFrame, frames=len(var)) + + diff --git a/notebooks/SliceThroughModel.ipynb b/notebooks/SliceThroughModel.ipynb index bc8548fb..1259ebf6 100644 --- a/notebooks/SliceThroughModel.ipynb +++ b/notebooks/SliceThroughModel.ipynb @@ -14,31 +14,16 @@ "from SimPEG import mesh, utils\n", "sz = [10,20,30]\n", "M = mesh.TensorMesh(sz)\n", - "mtrue = utils.ModelBuilder.randomModel(sz,its=20)\n", - "M.SliceVideo(utils.mkvc(mtrue), normal='x')" + "mtrue = utils.ModelBuilder.randomModel(sz,seed=786,its=20)\n", + "M.videoSlicer(utils.mkvc(mtrue), normal='x')" ], "language": "python", "metadata": {}, "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Warning: mumps solver not available.\n", - "Using a seed of: " - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - " 155\n" - ] - }, { "html": [ "" ], "metadata": {}, "output_type": "pyout", - "prompt_number": 1, + "prompt_number": 2, "text": [ - "" + "" ] } ], - "prompt_number": 1 + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "models = [utils.ModelBuilder.randomModel(sz,seed=786,its=i) for i in range(40)]" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def function(var, ax, clim, tlt, i):\n", + " p = M.slicer(var, normal='y', index=5, imageType='CC', ax=ax, clim=clim)\n", + " tlt.set_text('%d smoothing iterations'%(i))\n", + " return p\n", + " \n", + "M.video(models,function)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "html": [ + "" + ], + "metadata": {}, + "output_type": "pyout", + "prompt_number": 5, + "text": [ + "" + ] + } + ], + "prompt_number": 5 }, { "cell_type": "code",