output_notebook() is not defined - why? - python

Trying Bokeh for the first time. Using the following example code from http://docs.bokeh.org:
from collections import OrderedDict
from bokeh.charts import Scatter
from bokeh.sampledata.iris import flowers
output_notebook()
Inside iPython (Anaconda, py 3.4, Win 7), get the following error:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-c57c6fa8f51a> in <module>()
----> 1 output_notebook()
NameError: name 'output_notebook' is not defined
Why? This is straight from the examples.

It looks as if you forgot to import output_notebook.
Typical notebooks have following imports:
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
output_notebook()

Related

cannot import name 'MSELossFlat' from 'fastai.layers'

I am running the code https://github.com/thiagodma/Pytorch_exs/blob/master/MultiTaskLearning/multitask_age_gender_ethnicity_resnet34.ipynb on Colab.
As I write:
from fastai import *
from fastai.vision import *
from fastai.layers import MSELossFlat, CrossEntropyFlat
from torchvision import transforms
import warnings
warnings.filterwarnings("ignore")
files_train = get_image_files("utkface_aligned_cropped/UTKFace")
files_valid = get_image_files("utkface_aligned_cropped/crop_part1")
I get the error:
ImportError Traceback (most recent call last)
<ipython-input-4-07ceae9afcad> in <module>()
1 from fastai import *
2 from fastai.vision import *
----> 3 from fastai.layers import MSELossFlat, CrossEntropyFlat
4 from torchvision import transforms
5 import warnings
ImportError: cannot import name 'MSELossFlat' from 'fastai.layers' (/usr/local/lib/python3.7/dist-packages/fastai/layers.py)
This is probably because you are referring to a code snippet that used the fastai v2 library. In the newest fastai, the MSELossFlat function can be imported at fastai.losses.
But since you are trying out an old code snippet, it's best to reproduce the execution enviroment of that code to get an expected result. I would recommend trying to install fastai v2 on your environment and execute the code again.

Package colorednoise.py working in jupyter notebook but no script under the same environment

When I run the code:
import sys
import math
import numpy as np
import matplotlib.pyplot as plt
from typing import List, Tuple
import os
import scipy.io, scipy.signal
import colorednoise as cn
def generate_pink_noise(singal_length):
beta = 1
samples = singal_length
noise = cn.powerlaw_psd_gaussian(beta, samples)
return noise
I get:
Traceback (most recent call last):
File "...MEA_foward_model.py", line 11, in <module>
import colorednoise as cn
ModuleNotFoundError: No module named 'colorednoise'
Note the 'test' environment I'm using shown here when I get the error: environment.
However the same code runs without error in Jupiter notebook:
import colorednoise as cn
signal_length =10
beta = 1 # the exponent: 0=white noite; 1=pink noise; 2=red noise (also "brownian noise")
samples = signal_length # number of samples to generate (time series extension)
noise = cn.powerlaw_psd_gaussian(beta, samples)
This image shows (see top right corner) shows that the same environment is used
jupyter notebook output. What is causing this dissonance between the two different behaviours?

ImportError: cannot import name PolyDrawTool (Not able to Import - PolyDrawTool)

I am Trying to run this example (Not able to Import - PolyDrawTool)
https://docs.bokeh.org/en/latest/docs/user_guide/examples/tools_poly_draw.html
Python 2.7
bokeh v0.12
Got Import Error :
ImportError Traceback (most recent call last)
<ipython-input-1-cc6a3ae44bb8> in <module>()
1 from bokeh.plotting import figure, output_file, show
----> 2 from bokeh.models import PolyDrawTool
3
4 output_file("tools_poly_draw.html")
5
ImportError: cannot import name PolyDrawTool
PolyDrawTool is a very recent feature, it is much newer then version 0.12. You will need to update your Bokeh version to be able to use it. The latest current release is 0.12.15.

importerror cannot import name candlestick

I got an error message from Python:
Traceback (most recent call last):
File "stock_script.py", line 9, in <module>
from matplotlib.finance import candlestick
ImportError: cannot import name candlestick
Why?
Matplotlib code has been changed...
Go into the script and replace "candlestick" by "candlestick_ohlc"
So it should read:
from matplotlib.finance import candlestick_ohlc
candlestick_ohlc(ax1, ...
Not very nice changing the name and breaking existing code...
I have fixed this by changing the import like this.
from matplotlib.finance import candlestick_ohlc as candlestick
No further changes are then needed in the code.
You need to add an underscore to it to use this same library:
from matplotlib.finance import _candlestick

AttributeError: 'module' object has no attribute 'cbook'

I am trying to run a simple code and I have all the dependencies for matplotlib and numpy installed in my Canopy. Still I am getting error.
import cv2
import numpy as np
import matplotlib.pyplot as plt
x = cv2.imread('jay.jpg')
plt.imshow(x, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()
Error:
%run "c:\users\jay\appdata\local\temp\tmppvibq9.py"
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
c:\users\jay\appdata\local\temp\tmppvibq9.py in <module>()
2 import numpy as np
3 import matplotlib.cbook
----> 4 import matplotlib.pyplot as plt
5
6 x = cv2.imread('jay.jpg')
C:\Users\Jay\AppData\Local\Enthought\Canopy\System\Lib\site-packages\matplotlib\pyplot.py in <module>()
27 from cycler import cycler
28 import matplotlib
---> 29 import matplotlib.colorbar
30 from matplotlib import style
31 from matplotlib import _pylab_helpers, interactive
C:\Users\Jay\AppData\Local\Enthought\Canopy\System\Lib\site-packages\matplotlib\colorbar.py in <module>()
30
31 import matplotlib as mpl
---> 32 import matplotlib.artist as martist
33 import matplotlib.cbook as cbook
34 import matplotlib.collections as collections
C:\Users\Jay\AppData\Local\Enthought\Canopy\System\Lib\site-packages\matplotlib\artist.py in <module>()
9 import numpy as np
10 import matplotlib
---> 11 import matplotlib.cbook as cbook
12 from matplotlib.cbook import mplDeprecation
13 from matplotlib import docstring, rcParams
AttributeError: 'module' object has no attribute 'cbook'
Dependencies I have installed for numpy and matplotlib:
1.) libsvm-3.17.win64-py2.7
2.) pyparsing-2.0.3-1.win64-py2.7
3.) python-dateutil-2.4.2-2.win64-py2.7
4.) pytz-2015.7-1.win64-py2.7
5.) six-1.10.0-1.win64-py2.7
6.) scipy-0.13.3.win64-py2.7
7.) numpy-MKL-1.9.2-2.win64-py2.7
8.) Matplotlib 1.5.1-win64-py2.7
9.) pip 8.0.2-1.win64-py2.7
Try this:
Close your jupyter notebook and terminate ipython.
Restart Terminal
Enter this command in Terminal:
export LANG=en_US.UTF-8;export LC_ALL=en_US.UTF-8
Re-run your ipython / Jupyter notebook now. It works like a charm.
conda install matplotlib --force
I don't know for sure that this is causing your problem, but you are running your code in the wrong Python environment:
C:\Users\Jay\AppData\Local\Enthought\Canopy\System\
You should be running it in
C:\Users\Jay\AppData\Local\Enthought\Canopy\User
See
https://support.enthought.com/entries/23646538-Make-Canopy-User-Python-be-your-default-Python
and
http://docs.enthought.com/canopy/configure/faq.html#where-are-all-of-the-python-packages-in-my-user-python-environment
You can find which 'matplotlib.py' is imported and then open that file and check if it has 'cbook' or not.
import imp
imp.find_module("matplotlib")
i had the same problem and it was because of this line :
from sys import stdout
i deleted this import and the problem is gone now

Categories