How to fix this code to display images thumbnails - python-3.x

The following code is supposed to display thumbnails of different images that are available in the \data directory. However, something is wrong with it.
from os import listdir
from os.path import isfile, join
mypath = "data/"
file_names = [mypath+f for f in listdir(mypath) if isfile(join(mypath, f))]
number_files = len(file_names)
fig = plt.figure(figsize = (40,50))
fig.subplots()
axes = []
for i ,file_name in enumerate(file_names):
dataset = pydicom.dcmread(file_name)
axes.append(fig.add_subplot(int(math.sqrt(number_files))+1 , int(math.sqrt(number_files))+1, i+1))
plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
plt.show()
I have the following error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-73-3ed68a41a4d3> in <module>
10 dataset = pydicom.dcmread(file_name)
11 axes.append(fig.add_subplot(int(math.sqrt(number_files))+1 , int(math.sqrt(number_files))+1, i+1))
---> 12 plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
13 plt.show()
...
RuntimeError: The following handlers are available to decode the pixel data however they are missing required dependencies: GDCM (req. GDCM)
I've tried to do !conda install -c conda-forge gdcm -y but I've gotthe following error:
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: -
Found conflicts! Looking for incompatible packages.
This can take several minutes. Press CTRL-C to abort.
failed
UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:
Specifications:
- gdcm -> python[version='2.7.*|3.5.*|3.6.*|>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|3.4.*']
Your python: python=3.8
If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.
Any idea how to fix the error?

One alternative to GDCM that'll be available in the next released version of pydicom is pylibjpeg (disclosure: I'm a contributor to pydicom and author of pylibjpeg). You can use pylibjpeg with pydicom v2.0 simply by importing it before calling Dataset.pixel_array:
Installation
pip install pylibjpeg pylibjpeg-libjpeg
Usage
from pydicom import dcmread
import pylibjpeg
ds = dcmread('path/to/file.dcm')
arr = ds.pixel_array

If don't absolutely need python 3.8, you could try to create a new environment with a python 3.7 interpreter by using the following command line:
conda create -n myenv python=3.7
You could also try to install the dependency with pip from your existing environment.

Related

Finding right version of python/sklearn in to use machine learning model in pyenv

I pickled a model on Kaggle and tried to download it to run locally. Using poetry and pyenv I ran the following commands to create a project:
pyenv local 3.6.6
poetry new model_api
cd model_test
poetry env use python
poetry add "sklearn>=0.21.3"
but received the error below.
If I simply use sklearn and install it with poetry I get this error when executing my code in VS Code.
/bin/python /home/gary/Documents/model_api/model_api/app.py
Traceback (most recent call last):
File "/home/gary/Documents/model_api/model_api/app.py", line 5, in <module>
model = pickle.load(f)
ModuleNotFoundError: No module named 'sklearn.ensemble.forest'
This is the code I am attempting to run.
import sklearn
import pickle
f = open('./model/ForestModel','rb')
model = pickle.load(f)
I'm trying to use Python 3.6.6 and sklearn 0.21.3 based on what I'm seeing on Kaggle:
If I try to use a more current version of Python like 3.8.10 I get the same error. I think I'm missing something simple/obvious. Any pointers or things I could check would be greatly appreciated.
There is no package sklearn with the version you like to be installed. I think you are looking for scikit-learn instead (Docs).
You can install the most up to date version that is supported by your other dependencies by running:
poetry add scikit-learn
Or if you need to install a specific version:
poetry add "scikit-learn==0.24.2"
For other options, check out the poetry docs here.
I would try to use the Anaconda package manager instead of pyenv. You could create an environment with the following code:
conda create -n envName sklearn
And they would probably keep up with the best coordination of Python packages so that you don’t get errors.

Exception has occurred: ModuleNotFoundError No module named 'cv2'

I have code that requires: import cv2
but get the error message: Exception has occurred: ModuleNotFoundError
No module named 'cv2'
I have seen exactly the same question before, but all the suggestions fail. The question is 6 years old and hence am repeating it.
various failed suggestions:
conda install --channel https://conda.anaconda.org/menpo opencv3
conda install -c menpo opencv
I am using Windows 10 and have python 3.8.1 running.
I have removed cv2 from the code which works without error and resolves the issue.
The link here (at time of writing, version 4.1.2.3) describes the opencv-python package:
opencv-python link
With the following description of cv2 towards the bottom of the page:
It's easier for users to understand opencv-python than cv2 and it makes it easier to find the package with search engines. cv2 (old interface in old OpenCV versions was named as cv) is the name that OpenCV developers chose when they created the binding generators. This is kept as the import name to be consistent with different kind of tutorials around the internet. Changing the import name or behaviour would be also confusing to experienced users who are accustomed to the import cv2.

Numpy error when installing fbprophet Python on Windows 10

I'm trying to install fbprophet on my win10 64 bit machine (anaconda). I've followed the instructions here here and here, but I keep getting the same error:
ImportError Traceback (most recent call last)
<ipython-input-1-7dd3504c366f> in <module>
----> 1 import pandas as pd
~\Anaconda3\envs\fbprophet\lib\site-packages\pandas\__init__.py in <module>
15 if missing_dependencies:
16 raise ImportError(
---> 17 "Unable to import required dependencies:\n" + "\n".join(missing_dependencies)
18 )
19 del hard_dependencies, dependency, missing_dependencies
ImportError: Unable to import required dependencies:
numpy:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
1. Check that you expected to use Python3.7 from "C:\Users\Ran\Anaconda3\envs\fbprophet\python.exe",
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy version "1.17.0" you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: DLL load failed: The specified module could not be found.
I assume it has something regarding the numpy version, but the version in the virtual environment I've created for fbprophet is as required (1.17). It seems that this evnironment tries to use different version of numpy.
I have numpy installed in my anaconda base and in other virtual environments and there is nothing wrong with it. It happens only when I try to install fbprophet.
UPDATE:
I was able to solve the problem by uninstall numpy (v 1.17) and install instead numpy v1.16

ClobberError when trying to install the nltk_data package using conda?

I am trying to install nltk_data package to my environment natlang using conda by giving the following command:
(natlang) C:\Users\asus>conda install -c conda-forge nltk_data
I receive the following errors:
Verifying transaction: failed
CondaVerificationError: The package for nltk_data located at
C:\Users\asus\Anaconda3\pkgs\nltk_data-2017.10.22-py_0
appears to be corrupted. The path
'lib/nltk_data/corpora/propbank/frames/con.xml'
specified in the package manifest cannot be found.
ClobberError: This transaction has incompatible packages due to a shared
path.
packages: conda-forge::nltk_data-2017.10.22-py_0, conda-forge::nltk_data-
2017.10.22-py_0
path: 'lib/nltk_data/corpora/nombank.1.0/readme'
ClobberError: This transaction has incompatible packages due to a shared
path.
packages: conda-forge::nltk_data-2017.10.22-py_0, conda-forge::nltk_data-
2017.10.22-py_0
path: 'lib/nltk_data/corpora/nombank.1.0/readme-dictionaries'
ClobberError: This transaction has incompatible packages due to a shared
path.
packages: conda-forge::nltk_data-2017.10.22-py_0, conda-forge::nltk_data-
2017.10.22-py_0
path: 'lib/nltk_data/corpora/nombank.1.0/readme-nombank-proposition-
structure'
I am working on Anaconda 3, python version 3.6.5, windows 10 enterprise.
Can someone please tell me why this error is occurring and how can I fix it.
Background: I originally wanted to use punkt in one of my programs using the code lines:
import nltk_data
nltk.download()
This would open the nltk downloader and after installing all the packages including punkt, on further running the program I would still encounter the following error:
LookupError:
Resource [93mpunkt[0m not found.
Please use the NLTK Downloader to obtain the resource:
[31m>>> import nltk
>>> nltk.download('punkt')
I tried rerunning the nltk.donwload() and nltk.download('punkt') a couple of times with no change. So then I decided to simply install the nltk_data package to my environment based on the assumption that if I install the package to the env itself, I won't have to use the nltk.download function to use punkt.
Summarizing, I have the following two questions:
If I install the nltk_data package to my evn, do I still need to use the nltk.download function in my code? If yes, how do I resolve the lookup error?
If installing to the evn is enough, then how do I resolve the clobber error?
(ps: I apologize if this sounds stupid, I am very new to machine learning and working with python in general.)
The nltk_data repository is a collection of zipfiles and xml meta data. Usually, it is not installation through packaging tools such as conda or pip.
But there is this utility from conda-forge that tries to install the nltk_data, https://github.com/conda-forge/nltk_data-feedstock
To use it, on the terminal/command prompt/console, first add the conda-forge channel:
conda config --add channels conda-forge
Then you shouldn't need the -c option, and just use:
conda install nltk_data
Please try the above and see whether that get rids of the ClobberError.
This error is requesting you to download a specific nltk dataset call punkt:
Please use the NLTK Downloader to obtain the resource:
>>> import nltk
>>> nltk.download('punkt')
Running nltk.download() without specifying which specific dataset you want to download will call up a tkinter GUI which normally wouldn't be possible if you are accessing your machine remotely without a GUI.
If you're unsure of which resource you need, I would suggest using the popular collection.
import nltk
nltk.download('popular')
Answering 2 que first- there have been similar issues all across windows machines. Its better to use the ntlk.download() function if you want to use punkt or a similar module.
1) The lookup error can easily be resolved. It was because of a typo. Instead of
import nltk_data
it should be
import nltk.data

Unable to import sklearn and statsmodels from Anaconda from windows 10 pro

I'm relatively new to python, so please excuse my ignorance on what could be a very easy fix. I am running python 3.6 through the Rodeo IDE, and it has been great, as it is similar to R-Studio (which I am very familiar with). As an aspiring data scientist, I am trying to learn how to fit regression and time series models to data, and all of the tutorials that I have found all say that I need various packages, all of which should be included in the Anaconda library. After downloading and re-downloading Python, Rodeo, and Anaconda, and trying various online fixes, I have been unable to successfully load the scikit-learn and the statsmodels modules.
#here is everything I have tried.
#using pip
! pip install 'statsmodels'
! pip install 'scikit-learn'
! pip install 'sklearn'
I don't get any errors here, and to be honest I'm kind of confused as to what this actually does, but I have seen many people online always suggest that this is a big problem when trying to import modules.
#using import
import sklearn
import statsmodels
from sklearn import datasets
import statsmodels.api as sm
all of the above give me the same error:
import statsmodels.api as sm
ImportError: No module named 'statsmodels'
ImportError: Traceback (most recent call last)
ipython-input-184-6030a6549dc0 in module()
----> 1 import statsmodels.api as sm
ImportError: No module named 'statsmodels'
I have tried to set my working directory to the Anaconda 3 file that has all of the packages and rerunning the above code with no success.
I'm thinking that the most likely problem has to do with my inexperience, and it is probably a simple fix. Is it possible that the IDE is bad or anaconda just doesn't like me?
So keeping all of the above in mind, the question is, how can I import these modules successfully so that I can access their functionality?
Option 1:
After installing packages with pip, try closing and reopening your IDE/Jupyter Notebook and try again.
This is a known bug that Jake VanderPlas outlined here
Option 2:
Don't put quotations around your pip messages.
!pip install -U statsmodels
!pip install scikit-learn
Option 3:
Also are you using Anaconda? If you are, you should already have scikit-learn. If you are trying inside Rodeo, I think you need to set your path inside Rodeo. Open Rodeo and set the Python Path to your fresh anaconda. See here

Resources