Can't resolve warning while importing tensorflow in colab - tensorflow

It keeps on showing this yellow line under every import from Tensorflow in google colaboratory. I wonder why is this happening?
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
from tensorflow.keras.model import Sequential

I'm experiencing this too. It's a bug in TensorFlow. If you try executing the code everything will work fine, it's just a warning. However, if you want to make the warning go away you can replace all imports that start with tensorflow.keras with keras.api._v2.keras.
In your case:
from keras.api._v2.keras import layers
from keras.api._v2.keras.layers.experimental import preprocessing
from keras.api._v2.keras import Sequential

Related

import "tensorflow.keras.applications" could not be resolved (reportMissingImports) in google-colaboratory

I am trying to import tensorflow.keras.applications in Google Colab. The imported module works fine. There is no compilation error. But, I am being shown a yellow curved underline, kind of a warning.
Error:
What is the problem that leads to such a warning?
Thank you.
You are not the only one experiencing this, and it does not happen only in Google Colab. It is a bug in Tensorflow.
Since it is just a warning you could ignore it. However if you like having code completion like I do you can substitute your imports from this:
import tensorflow.keras.x
To this:
import keras.api._v2.keras.x
Where x is the import you want to get.

Import "tensorflow.keras.optimizers" could not be resolved(reportMissingImports)

Can you help me to resolve my problem?
I believe this is just a bug in Google Colab. If you try the import below it says the same:
import tensorflow.keras
However if you try using the import everything works. For example:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
test=ImageDataGenerator(rescale=1./255)
Even code completion works as it should

Why do the recommended import statements for Docusaurus fail?

Importing react components into a Docusaurus markdown file has a common pattern like:
import Figure from '#site/src/components/figure' github source
import BrowserWindow from '#site/src/components/BrowserWindow' official docs
However, this pattern throws an error in my application when I include the following line in docs/theirlabel/latest.md:
import Figure from '#site/src/components/figure'
Throwing the following error
ERROR in ./docs/theirlabel/latest.md 1:991-1039
Module not found: Error: Can't resolve '#site/src/components/figure'
in '/home/projects/github-5c7qs5-wquhko/docs/theirlabel'
Commenting that line allows the site to compile correctly.
I've tried many variations of referencing/including the figure.jsx component without success, like:
import Figure from '/src/components/figure'
import Figure from './src/components/figure'
import Figure from '../src/components/figure'
import Figure from 'src/components/figure'
import Figure from './figure'
import Figure from '/figure'
Any ideas what I'm doing wrong?

Using matplotlib with Qt5 backend with an existing QApplication running, in the same process

We have a Qt5 application that uses PySide2. Recently, we wanted to show plots using matplotlib while our PySide2 application is running in the same process (in a different thread) but then matplotlib crashes (when using PySide2) or freezes before drawing (when using PyQt5).
Here is a minimal sample, uncomment the 23rd line to make matplotlib crash or freeze:
from threading import Thread
from PySide2.QtWidgets import QApplication, QLabel
import matplotlib
matplotlib.use('QT5Agg')
import matplotlib.pyplot as plt
def start_qt_app():
t = Thread(target=qt_app_thread_func)
t.start()
def qt_app_thread_func():
app = QApplication()
label = QLabel("Hello World")
label.show()
app.exec_()
# Uncomment the line below to make matplotlib crash.
#start_qt_app()
plt.plot([1, 2, 3, 4])
plt.show()
input("Press enter to quit.")
print("Finished.")
My guess is that this is related to the limitation that we can only have 1 QApplication running in a process. So this causes something to go wrong in matplotlib.
How can I solve this problem? One solution that comes to my mind is to create a proxy object for matplotlib that runs matplotlib in another process but I am not sure if this would be the least labor intensive solution. Maybe I can somehow make matplotlib use the existing QApplication? I cannot run our PySide2 app in another process since it uses dynamically created numpy arrays to be passed from main thread to the GUI, and it would cost performance to start it in another process.
As #ImportanceOfBeingErnest points out, matplotlib can live with Qt as the official example shows: Embedding in Qt.
Example:
from PySide2.QtWidgets import QApplication, QLabel
import matplotlib
matplotlib.use('QT5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
if __name__ == '__main__':
app = QApplication()
label = QLabel("Hello World")
label.show()
canvas = FigureCanvas(Figure(figsize=(5, 3)))
ax = canvas.figure.subplots()
ax.plot([1, 2, 3, 4])
canvas.show()
app.exec_()
you should use asyncqt to manage ui and application threads.from asyncqt QEventLoop is a way you can accomplish with
await loop.run_in_executor(exec, method_name)
asyncqt is a spin off library from quamash which is written for PyQt5. example codes are same. So upon your preference, you may use asyncqt with PySide2 or quamash with PyQt5 to make your app responsive when background tasks are running.
asyncqt examples
asyncqt 0.7.0 pypi
Quamash 0.6.1

Google Colab: Why matplotlib has a behaviour different then default after importing pandas_profiling?

When using a notebook in Google Colab, my matplotlib plots have different behaviors whether I import the library pandas-profiling or not.
If I do not import pandas-profiling, the plots are displayed inline by default. But if I import the library, the plots stop being displayed inline.
Workarounds (possible solutions)
Updating the pandas-profiling library before importing it resolves the problem.
Adding %matplotlib inline after importing pandas-profiling resolves the problem.
Reproducing
Run this code in Google Colab to reproduce the problem. Test it with and without importing pandas_profiling. For each test, you will need to terminate the session (Runtime->Manage Sessions->Terminate). Just restarting the runtime is not enough.
import matplotlib.pyplot as plt
# importing the pandas_profiling makes matplotlib
# to stop showing the plot inline
# import pandas_profiling
plt.plot([1, 2], [1, 2])
The expected behavior is to show the plot inline by default, but after importing pandas-profiling, the plots stop being displayed inline.
The real problem
I stumbled upon this problem when my seaborn plotting functions started to break.
For example, consider the following code.
import matplotlib.pyplot as plt
# import pandas_profiling
import seaborn as sns
def plot():
ax = sns.pointplot([1, 2], [1, 2])
print(len(ax.collections))
Now call plot() in two different jupyter cells.
Without pandas-profiling: each function call will print 1.
With pandas-profiling: each function call will add 1 to the previous output.
The problem is in the version of pandas_profiling that is installed by default in Google Colab. The installed version (1.4.1) used to change the matplotlib's backend to agg when imported. The default matplotlib's backend in Colab is module://ipykernel.pylab.backend_inline.
We can see that by running the following before and after importing pandas-profiling:
import matplotlib
matplotlib.get_backend()
This behavior was changed in pull-request #125, which stops changing matplotlib's backend.
Until Google Colab updates the installed version, manually updating it seems to be the best solution. Just run the following in your Colab Notebook:
!pip install --upgrade pandas_profiling