How to read CDF file with python on mac? - python

I have installed spacepy using the following command (MacOS Mojave):
pip install git+https://github.com/spacepy/spacepy.git
But I keep getting this error while trying to from spacepy import pycdf:
Exception: Cannot load CDF C library; checked . Try 'os.environ["CDF_LIB"] = library_directory' before import.
I have tried following the directions from their website, but it seems like those directions are a bit outdated.

You can do something like:
import os
os.environ["CDF_LIB"] = "~/CDF/lib" # Give the path of the cdf library
# e.g. in this case the library is in your home directory.
Before doing:
from spacepy import pycdf
Source of the error can be found on this SpacePy website.
The cdf library is available here.
Basically, you need to set up the CDF_LIB (if pycdf has a trouble finding it) before importing the module. You can read the documentation here.

Related

How to use R package in python with importr package

I'm working on a web application using Dash, and I would like to use arules and aruleViz from R within a python script to get a graph of association rules obtained by using an Aprioi algorithm.
I found the rpy2 package and I installed it using conda conda install rpy2 ,then I try to import some packages like base tools by:
from rpy2.robjects.packages import importr`
arules = importr("tools",)`
That was fine(the package was imported)
And when I use: arules = importr("arules",) or arules = importr("arulesViz",)
I got receive the following error:
RRuntimeError: Error in loadNamespace(name) : there is no package called ‘arulesViz’
I saw an option in the importrpackage (lib_loc=None). I'm not sure how can I change it.
If there is any way to solve this, or if you know of a package in python that will help me plot a graph with vertices (I know how to so that with matplot.lib library using scatter but i'm not happy with it) I would greatly appreciate the help!
Thank you!
R packages must be installed before rpy2 can hope to find them. rpy2's importr() relies on R's own package loading system.
If you are certain to have installed that package earlier, you might have installed it in different directory, in a previous R process, and your current R process does not know about that directory. The optional named argument lib_loc accepts the path to that directory (see the doc).
(Note that there is also a utility function to check if an R package is installed without loading it)

Can't load datasets from linearmodel python package

I am trying to learn the linearmodels package for python.
I want to do this by practicing with the data sets, as can be seen here.
Example code:
import numpy as np
from linearmodels.iv import IV2SLS
from linearmodels.datasets import mroz
data = mroz.load()
But my code breaks when i run data = mroz.load()
error message:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\...\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\linearmodels\\datasets\\mroz\\mroz.csv.bz2'
I have pip version: 19.1.1
Conda can't find the package at all
and i have the latest version of linearmodels package: 4.13
The folder specified in the error message i can find, i.e. datasets\mroz but not the csv.bz2 file.
The same holds for every other data set i try to open.
Why am i not able to open the datasets?
let me know if you need additional information.
This is a bug in the package. If you download and unpack the source distribution you would find it lacks all *.csv.bz2.
I see two problems in the package. First, MANIFEST.in lists *.csv.bz. It must be *.csv.bz2 or *.csv.bz*.
Second, they tried to add the datasets in setup.py but also failed, not sure why. Perhaps the files must be declared as belonged to different subpackages, not to the main package.
Please report the bugs to the issue tracker.

Google Colaboratory - AttributeError: module X has no attribute Y

I'm trying to use the Freesound API in a Google Colaboratory notebook (running Python 3) to generate a database of sounds for which to do machine learning with. However I have been unable to use the definitions in a module I imported.
I've looked at other similar questions but they did not seem to address my issue (most were cases of trying to import a module in the standard python library and instead importing a .py file of the same name) and I apologize if this particular issue has been covered somewhere else.
The boilerplate code is as follows:
#clone relevant Git repo
!git clone https://github.com/MoltenMuffins/freesound-python
!ls
#Import packages
import os
import sys
import requests
#Open module file and import module
open('freesound.py','wb')
import freesound
There is some code after that but it is not relevant to the issue. Running this last code block is what gives me the Attribute Error despite FreesoundClient being defined in the freesound.py file cloned from the repo:
freesound_client = freesound.FreesoundClient()
I would greatly appreciate an explanation regarding this issue!
Here's a link to the colabs notebook if you'd like to take a look
I would follow the repo's instructions of using their setup.py to do the installation:
After cloning the git repo, you want to change your working directory to the freesound-python directory and run setup.py
import os
os.chdir('/content/freesound-python')
!python setup.py install
# now import the module
import freesound

Cannot import name active_contour

I am studying a beginners guide on a sci-image, I successfully downloaded scikit-image via easy_install when I tried the code as shown on this site:
http://scikit-image.org/docs/dev/auto_examples/edges/plot_active_contours.html#example-edges-plot-active-contours-py
I get an error stating that from skimage.segmentation import active_contour has an error:
ImportError: cannot import name active_contour
My first solution, as what I have read on the internet is to delete the library I am using and re-installing it using pip_install however I still get the same result. Any advice on this thanks...
The active_contour_model is only avaiable in the development version of skikit-image and will (probably) be included in the skimage v.0.12 but until then you need to manually install the development version found on https://github.com/scikit-image/scikit-image.
The documentation of the stable version of skimage.segmentation is here: http://scikit-image.org/docs/stable/api/skimage.segmentation.html

Python script ImportError: No module named celltool.simple_interface

so I'm on a Windows 8 and I'm doing some image analysis for my research. It requires running this python script (I did not write it) called "calculate_distances.py". I am also using CellTool (http://pantheon.yale.edu/~zp2/Celltool/) and Numpy which I have successfully installed.
When I try to run my calculate_distances script, I get this error:
Traceback (most recent call last):
file "calculate_distances.py" line 4, in <module>
import celltool.simple_interface as si
ImportError: No module named celltool.simple_interface
Here is my calculate_distances.py script:
#!/Library/Frameworks/Python.framework/Versions/2.5/bin/python
# Import various celltool modules
import celltool.simple_interface as si
import celltool.contour.contour_class as contour_class
from celltool.utility.path import path
import celltool.utility.datafile as datafile
import numpy
# Designate your working directory - ie, replace '/parent_directory/' with the path that contains your contour files
#parent_dir=path('/parent_directory/')
parent_dir=path('./')
# Identify and load all of the contour files
contour_files=parent_dir.files('*.contour')
contours=si.load_contours(contour_files)
all_distances=numpy.arange(len(contours[0].points))
# Calculate the normal displacement of the cell boundary at every contour point.
n=1
while n<len(contours):
contour_class.Contour.global_reorder_points(contours[n],contours[n-1])
displacement_vectors = contours[n-1].points - contours[n].points
normals = contours[n-1].inward_normals()
distances = (normals * displacement_vectors).sum(axis=1)
all_distances=numpy.vstack((all_distances,distances))
n=n+1
# Save a csv file with the normal displacement of the boundary at each contour point (columns) at each time point (rows)
datafile.write_data_file(all_distances,parent_dir/'distances.csv')
I have no idea what the error means, as I'm a first-time programmer. First I thought I needed to install a C++/fortran compiler, so I installed Simple Fortran, but it still does not work. Everything works fine on Mac system (after I installed GNU fortran) but for some reason it's not working on Windows. Is it because my CellTool is not installed in the right location? Or is it something with the script?
Any ideas? Thanks very much for your help!!
You have two options then.
Try adding CellTool installation directory to your PYTHONPATH environment variable, if you haven't already (create this variable if necessary).
Search for something like celltool.dll or libcelltool.dll (or even celltool.py). See if it helps. If not :
Compile the source by following those few steps... Install MinGW (installer here), checking C/C++ (gcc/g++) and Fortran when asked by the installer. Then add C:/MinGW/bin (or whereever you installed MinGW) to your PATH environment variable. Download and unpack CellTool source package from the web site. Finally open a command shell (search for cmd program), change directory to the newly unpacked directory and build by typing :
python setup.py install
This should properly compile and install the celltool python package to the site-packages directory of your Python installation.
Option 2 is the sure/safe (but not easiest, I agree) way to get it work.

Categories