MTgeophysics / mtpy

Python toolbox for standard Magnetotelluric (MT) data analysis
GNU General Public License v3.0
146 stars 66 forks source link

numpy import replaced with NoneType #19

Closed alkirkby closed 6 years ago

alkirkby commented 6 years ago

Somewhere in mtpy, np is set to None, so that when you want to call numpy functions, it can't find them currently trying to run the following code in spyder, it usually happens after I have run it more than once.


sys.path.append('/c/git/mtpy')

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import LogLocator
from matplotlib import colors
# Import required classes from key mtpy modules 
from mtpy.imaging2.seismic import Segy, VelocityModel
from mtpy.utils.modem_slice import MODEM_slice

import os.path as op

master_workdir = r'C:\Users\u64125\OneDrive - Geoscience Australia\Reprocessing_2016'
seismic_dir = op.join(master_workdir,'seismic')
segy_fn = op.join(seismic_dir,'mig_09ga_ga1_20s.sgy')
stacking_velocity_fn = op.join(seismic_dir,'dmovel_ga1.txt')

sl = Segy(segy_fn)
vm = VelocityModel(stacking_velocity_fn, ni=20)

model_dir = op.join(master_workdir,'ModEM','GBinv34tip2')
m_fn = op.join(model_dir,'Modular_MPI_NLCG_070.rho')
d_fn = op.join(model_dir,'ModEM_Data.dat')

ms = MODEM_slice(model_fn=m_fn, data_fn=d_fn)

PLOT_NTRACE = 1500 # number of traces to be plotted
S_MAX_DEPTH = 60e3 # maximum depth
NDEPTHS = 4000 # number of depth intervals (NDEPTHS), which should be ~ the number of samples in each trace

distances = sl.getDistances() # distance of each trace from the start of the 2D line

# define 1D grids for depth and distance, based on parameters defined above
gdepth    = np.linspace(0, S_MAX_DEPTH, NDEPTHS)
gdistance = np.linspace(0, np.max(distances), PLOT_NTRACE)

# fetch a depth-migrated image (done using velocity model defined earlier)
mdepth, mdist, svals = sl.getMigratedProfile(vm, gdepth, gdistance, nn=1)

# set plot dimensions
matplotlib.rcParams['figure.figsize'] = (10.0, 5.0)
fig,ax = plt.subplots(1,1)

# retrieve x coordinates of traces fetched above
gx        = np.array([sl.getAttribute('x', d) for d in gdistance])

# retrieve y coordinates of traces fetched above
gy        = np.array([sl.getAttribute('y', d) for d in gdistance])

# since the resistivity model has log-spacing in z, it would be an 
# overkill to sample it as finely as the in the seismic profile -- so, 
# instead, we use the z values in resistivity model
gz        = ms._m.grid_z

# create a list of coordinates that define the profile
mgxyz = []
for z in gz:
    for x,y in zip(gx, gy):
        mgxyz.append([x, y, z])

mgxyz = np.array(mgxyz)

# fetch slice, smoothed using 10 nearest neighbours 
mvals = ms.get_slice(mgxyz, nn=1)

# reshape the resistivity array
mvals = np.reshape(mvals, (gz.shape[0], gx.shape[0]))

# plot resistivity ==================================================
ci = ax.pcolor(gdistance, gz, mvals, 
               norm=colors.LogNorm(),
               vmin=np.power(10,0.5), vmax=np.power(10,7),
               cmap='bwr_r', 
               alpha=0.5, linewidth=0, 
               edgecolors='None',
               rasterized=True)

# deal with white stripes
ci.set_antialiaseds(True)
ci.set_rasterized(True)

# plot colorbar. Note that we explicitly set alpha value to 0.5, to account
# for darker colors in the plot that result from calling contourf twice.
cb_ax = fig.add_axes([0.25, 0.25, 0.5, 0.025])
cb = plt.colorbar(ci, cax=cb_ax, ticks = LogLocator(subs=range(10)), 
                  orientation="horizontal")
cb.solids.set_edgecolor('none') 
cb.solids.set_antialiased(True)
cb.solids.set_rasterized(True) 

# set max depth to 60 km, because the resistivity model goes much deeper
M_MAX_DEPTH = 60e3

# Plot seismic ======================================================   
# compute the 99th percentile and zero out all values below that. This can
# be tweaked to plot the required amount of detail without cluttering the
# plot

vmm = np.percentile(svals.flatten(), 99) 
svalsClipped = np.array(svals)
svalsClipped[svalsClipped<vmm]=0
ci = ax.contourf(mdist, mdepth, svalsClipped, 50, 
                 vmin=-vmm, vmax=vmm,
                 cmap='Greys', alpha=0.15, rasterized=True)

ax.invert_yaxis()
ax.set_aspect(1)
ax.set_ylim(M_MAX_DEPTH) 

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\u64125\AppData\Local\Continuum\Miniconda2\envs\mtpy27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)
  File "C:\Users\u64125\AppData\Local\Continuum\Miniconda2\envs\mtpy27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/u64125/OneDrive - Geoscience Australia/Reprocessing_2016/python/plot_resistivity_seismic.py", line 52, in <module>
    mdepth, mdist, svals = sl.getMigratedProfile(vm, gdepth, gdistance, nn=1)
  File "C:\Git\mtpy\mtpy\imaging2\seismic.py", line 146, in getMigratedProfile
    mdepths, mdistances = np.meshgrid(depths, distances)
AttributeError: 'NoneType' object has no attribute 'meshgrid'

Your Environment

Installed Python Packages: alabaster 0.7.10 py27_0 conda-forge asn1crypto 0.22.0 py27_0 conda-forge babel 2.4.0 py27_0 conda-forge backports.shutil_get_terminal_size 1.0.0 py27_1 conda-forge backports_abc 0.5 py27_0 conda-forge bleach 2.0.0 py27_0 conda-forge ca-certificates 2017.7.27.1 0 conda-forge certifi 2017.7.27.1 py27_0 conda-forge cffi 1.10.0 py27_0 conda-forge chardet 3.0.4 py27_0 conda-forge click 6.7 py27_0 conda-forge click-plugins 1.0.3 py27_0 conda-forge cligj 0.4.0 py27_0 conda-forge colorama 0.3.9 py27_0 conda-forge configparser 3.5.0 py27_0 conda-forge cryptography 2.0.3 py27_0 conda-forge curl 7.49.1 vc9_2 [vc9] conda-forge cycler 0.10.0 py27_0 conda-forge decorator 4.1.2 py27_0 conda-forge descartes 1.1.0 py27_0 conda-forge docutils 0.14 py27_0 conda-forge entrypoints 0.2.3 py27_1 conda-forge enum34 1.1.6 py27_1 conda-forge expat 2.2.1 vc9_0 [vc9] conda-forge fiona 1.7.9 py27_1 conda-forge freetype 2.7 vc9_1 [vc9] conda-forge freexl 1.0.2 vc9_2 [vc9] conda-forge funcsigs 1.0.2 py_2 conda-forge functools32 3.2.3.2 py27_1 conda-forge future 0.16.0 py27_0 conda-forge gdal 2.1.3 py27_vc9_5 [vc9] conda-forge geopandas 0.3.0 py27_0 conda-forge geos 3.5.1 vc9_1 [vc9] conda-forge hdf4 4.2.12 vc9_0 [vc9] conda-forge hdf5 1.8.18 vc9_1 [vc9] conda-forge html5lib 0.999999999 py27_0 conda-forge icu 58.1 vc9_1 [vc9] conda-forge idna 2.5 py27_0 conda-forge imagesize 0.7.1 py27_0 conda-forge ipaddress 1.0.18 py27_0 conda-forge ipykernel 4.6.1 py27_0 conda-forge ipython 5.4.1 py27_0 conda-forge ipython_genutils 0.2.0 py27_0 conda-forge ipywidgets 7.0.5 py27_0 conda-forge jedi 0.10.2 py27_0 conda-forge jinja2 2.9.6 py27_0 conda-forge jpeg 9b vc9_0 [vc9] conda-forge jsonschema 2.5.1 py27_0 conda-forge jupyter 1.0.0 py27_0 conda-forge jupyter_client 5.1.0 py27_0 conda-forge jupyter_console 5.2.0 py27_0 conda-forge jupyter_core 4.3.0 py27_0 conda-forge kealib 1.4.7 vc9_2 [vc9] conda-forge krb5 1.14.2 vc9_0 [vc9] conda-forge libiconv 1.14 vc9_4 [vc9] conda-forge libnetcdf 4.4.1.1 vc9_6 [vc9] conda-forge libpng 1.6.28 vc9_1 [vc9] conda-forge libpq 9.6.3 vc9_0 [vc9] conda-forge libspatialindex 1.8.5 vc9_1 [vc9] conda-forge libspatialite 4.3.0a vc9_15 [vc9] conda-forge libtiff 4.0.6 vc9_7 [vc9] conda-forge libxml2 2.9.3 vc9_9 [vc9] conda-forge libxslt 1.1.29 vc9_5 [vc9] conda-forge lxml 3.8.0 py27_0 conda-forge markupsafe 1.0 py27_0 conda-forge matplotlib 1.5.3 np112py27_8 conda-forge mistune 0.7.4 py27_0 conda-forge mkl 2017.0.3 0 mock 2.0.0 py27_0 conda-forge munch 2.2.0 py27_0 conda-forge nbconvert 5.2.1 py27_1 conda-forge nbformat 4.3.0 py27_0 conda-forge notebook 5.2.2 py27_1 conda-forge numpy 1.12.1 py27_0 obspy 1.0.3 py27_1 conda-forge olefile 0.44 py27_0 conda-forge openjpeg 2.2.0 vc9_0 [vc9] conda-forge openssl 1.0.2l vc9_0 [vc9] conda-forge pandas 0.20.3 py27_1 conda-forge pandoc 1.19.2 0 conda-forge pandocfilters 1.4.1 py27_0 conda-forge pathlib2 2.3.0 py27_0 conda-forge pbr 3.1.1 py27_0 conda-forge pcre 8.39 vc9_0 [vc9] conda-forge pep8 1.7.0 py27_0 conda-forge pickleshare 0.7.4 py27_0 conda-forge pillow 4.2.1 py27_1 conda-forge pip 9.0.1 py27_0 conda-forge proj4 4.9.3 vc9_4 [vc9] conda-forge prompt_toolkit 1.0.15 py27_0 conda-forge psutil 5.2.2 py27_0 conda-forge psycopg2 2.7.3 py27_0 conda-forge pycparser 2.18 py27_0 conda-forge pyflakes 1.6.0 py27_0 conda-forge pygments 2.2.0 py27_0 conda-forge pyopenssl 17.2.0 py27_0 conda-forge pyparsing 2.2.0 py27_0 conda-forge pyproj 1.9.5.1 py27_0 conda-forge pyqt 4.11.4 py27_2 conda-forge pysal 1.13.0 py27_0 conda-forge pysocks 1.6.7 py27_0 conda-forge python 2.7.13 1 conda-forge python-dateutil 2.6.1 py27_0 conda-forge pytz 2017.2 py27_0 conda-forge pyyaml 3.12 py27_1 conda-forge pyzmq 16.0.2 py27_2 conda-forge qt 4.8.7 vc9_6 [vc9] conda-forge qtconsole 4.3.1 py27_0 conda-forge requests 2.18.4 py27_1 conda-forge rope 0.10.7 py27_0 conda-forge rtree 0.8.3 py27_0 conda-forge scandir 1.5 py27_1 conda-forge scipy 0.19.1 np112py27_0 setuptools 36.3.0 py27_0 conda-forge shapely 1.6.1 py27_1 conda-forge simplegeneric 0.8.1 py27_0 conda-forge singledispatch 3.4.0.3 py27_0 conda-forge sip 4.18 py27_1 conda-forge six 1.10.0 py27_1 conda-forge snowballstemmer 1.2.1 py27_0 conda-forge sphinx 1.6.3 py27_0 conda-forge sphinxcontrib-websupport 1.0.1 py27_0 conda-forge spyder 2.3.9 py27_2 conda-forge sqlalchemy 1.1.13 py27_0 conda-forge sqlite 3.13.0 vc9_1 [vc9] conda-forge ssl_match_hostname 3.5.0.1 py27_1 conda-forge testpath 0.3.1 py27_0 conda-forge tornado 4.5.2 py27_0 conda-forge traitlets 4.3.2 py27_0 conda-forge typing 3.6.2 py27_0 conda-forge urllib3 1.22 py27_0 conda-forge vc 9 0 conda-forge vs2008_runtime 9.00.30729.5054 0 wcwidth 0.1.7 py27_0 conda-forge webencodings 0.5 py27_0 conda-forge wheel 0.29.0 py27_0 conda-forge widgetsnbextension 3.0.8 py27_0 conda-forge win_inet_pton 1.0.1 py27_1 conda-forge win_unicode_console 0.5 py27_0 conda-forge wincertstore 0.2 py27_0 conda-forge xerces-c 3.1.4 vc9_3 [vc9] conda-forge yaml 0.1.6 vc9_0 [vc9] conda-forge zlib 1.2.8 vc9_3 [vc9] conda-forge

geojunky commented 6 years ago

This has now been fixed; d04df82

alkirkby commented 6 years ago

It just happened to me again, this time 'numpy' has been set to NoneType. Same code. Not sure what's going on here...

geojunky commented 6 years ago

Please take a look at this:

https://stackoverflow.com/questions/44061754/package-load-error-in-anaconda-and-spyder

It looks like this may be what's happening and may be something specific to spyder.

geojunky commented 6 years ago

Here's a remedy perhaps:

https://stackoverflow.com/questions/13876306/spyder-umd-has-deleted-module

I don't have spyder installed, so can't really test immediately..

alkirkby commented 6 years ago

Thanks, I will try this.

YingzhiGou commented 6 years ago

please note that this UMD and using reload in the python code could also be the cause of the "broken inheritance" issues, that, sometimes, an error message is displayed saying

unbound method must be called with instance

for all the plotting classes under mtpy.imaging which inherits mtpy.imaging.mtplottools.PlotSettings, mtpy.imaging.mtplottools.MTArrows and/or mtpy.imaging.mtplottools.MTEllipse.

geojunky commented 6 years ago

Done.