I have a boolean subsetting function that works on a pandas dataframe:
def operational(df):
return df[(df['cf'] != 0) & (df['ta'] > 0)]
it works well in a script and in a jupiter notebook, when entered in cell:
#df = ...
df2 = operational(df)
However, if I keep function definiton in pick.py and import in to jupyter, things go unexpected. It seems upon import of a module jupiter does not reconise the types of variables inside a function:
import pick
pick.operational(df).head()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-374442fd7c0a> in <module>()
1 import pick
----> 2 pick.operational(df).head()
C:\Users\Евгений\Documents\GitHub\sandbox\pick.py in operational(_df)
11
12 def operational(df):
---> 13 return df[(df['cf'] != 0) & (df['ta'] > 0)]
14
15
TypeError: 'method' object is not subscriptable
Is importing something to notebook a generally bad idea? I use jupiter lab, if that matters.
Okay, from the comments it sounds like you were expecting the notebook to automatically pick up changes in an imported script. By default, Python caches imports, so most of the time changes to imported modules won't get picked up until you restart Python.
Fortuitously, Jupyter notebooks run on IPython, which supplies a code reloading cell magic. All you need to do is run:
%autoreload 2
in any cell, and your notebook should, from then on, automatically pick up any changes in pick.py as you make them (you still have to save the changes to disk before the reload magic can see them, of course).
Related
I'm trying to run SPM functions via Nipype but i get the same error when calling different SPM functions.
I can successfully connect to the Matlab compiler runtime and execute commands via nipype like so:
import nipype.interfaces.matlab as matlab
mlab = matlab.MatlabCommand()
mlab.inputs.script = 'spm ver'
mlab.run()
I can also import spm from nipype interfaces:
from nipype.interfaces import spm
matlab_cmd = "opt/MATLAB Runtime/v99/toolbox/spm12_r7771/spm12/run_spm12.sh opt/MATLAB/MATLAB Runtime/v99 script"
spm.SPMCommand.set_mlab_paths(matlab_cmd=matlab_cmd, use_mcr=True)
However, when i run the following (or other spm functions), e.g.
spm.SPMCommand().version
I end up with the same error, see:
ValueError Traceback (most recent call last)
File C:\Anaconda\envs\ConcSpace\lib\site-packages\nipype\utils\spm_docs.py:49, in
_strip_header(doc)
48 try:
---> 49 index = doc.index(hdr)
50 except ValueError as e:
ValueError: substring not found
The above exception was the direct cause of the following exception:
full error log
I'm not sure its because of missmatching versions of MCR or wrong paths i set?
I have Matlab2020b as well as the MCR installed separately on a win10 machine.
Nipype is up to date and running on a conda env in jupyterlab.
Glad for any tips that could fix it.
Thanks.
I have a file called "helper_functions.py":
import numpy as np
def arr_to_mat(arr):
arr_copy = arr.copy()
arr_len = arr_copy.size
n = int(np.ceil(np.sqrt(arr_len)))
arr_copy.resize((n, n))
return arr_copy
Which I call in a large jupyter notebook, where numpy has been loaded in at the beginning too. I then call the function, but it doesn't seem to load in numpy in that external script and functions:
import numpy as np
from helper_functions import arr_to_mat
m = np.random.rand(1, 25)
m_arr = arr_to_mat(m)
Then error:
~\helper_functions.py in arr_to_mat(arr)
40 arr_copy = arr.copy()
41 arr_len = arr_copy.size
---> 42 n = int(np.ceil(np.sqrt(arr_len)))
43 arr_copy.resize((n, n))
44 return arr_copy
NameError: name 'np' is not defined
Other functions have been working fine calling them from the external file, except this one doesn't work. Not sure why that is and I can't see exactly why either.
I will have more functions that have similar methods of importing modules and being called externally later, so I would like to have a solution for the futue to make sure I don't make this mistake again.
Many similar posts on here describe the issue being the function being called before the module has been loaded in, but this has not been the case for me as I both load in the module before the function, and the module has been loaded in in the jupyter notebook already as well and has been working.
The other answers simply say to restart the kernel and then it should work, but that didn't work either. The issue is still present.
I'm learning how to use qiskit and I'm using the jupyter notebook, but everytime I try to visualize the circuit with the attribute draw I get this error:
import qiskit
from qiskit import *
from qiskit import IBMQ
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
circuit = QuantumCircuit(qr, cr)
%matplotlib inline
circuit.draw(output='mpl')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-bd220039ee1c> in <module>
----> 1 circuit.draw(output='mpl')
AttributeError: module 'qiskit.circuit' has no attribute 'draw'
I also try applying a Hadamard gate and I get:
circuit.h(qr(0))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-59-c8b4318b743b> in <module>
----> 1 circuit.h(qr(0))
AttributeError: module 'qiskit.circuit' has no attribute 'h'
It seems that there is a name conflict. It is taking the circuit in from qiskit import circuit instead of circuit = ....
You just probably need to restat your notebook kernel.
Try another name for your circuit variable, right now python thinks you want the qiskit.circuit module to draw something. QuantumCircuit objects are the ones that have a draw method. You can see these two objects here if you call both, note I put one qubit and classical bit in the QuantumCircuit just per example as well you do not need the dots here it is just to make it more clear, just running circuit and QuantumCircuit(1,1) respectively would yield the same result.
You would get desired results if you tried a different variable name:
When I try using the variable name circuit it works for me, but try to use descriptive variable names that also could never be confused with modules or classes from the packages you import.
Also all your import statements can be combined into 1:
from qiskit import *
The star lets you import everything from qiskit including IBMQ. It can help you save a line or two.
I'm trying to get Reticulate working in RMarkdown, per the setup instructions. However, I am unable to share state between separate Python cells or Python and R cells, as the docs indicate I should be able to. Here is my setup and output:
Cell 1 (Setup):
{r}
library(reticulate)
path_to_python <- "/Users/User/anaconda3/bin/python"
use_python(path_to_python)
knitr::knit_engines$set(python = reticulate::eng_python)
py_available(initialize = TRUE)
Output:
[1] TRUE
Cell 2 (set variable in Python):
{python}
x = 2
Cell 3 (attempt to access Python variable in R):
{r}
py$x
Output:
Error in py_get_attr_impl(x, name, silent) : AttributeError: module '__main__' has no attribute 'x'
Cell 4 (set variable in R):
{r}
x <- 2
Cell 5 (attempt to access R variable in Python):
{python}
r.x
Output:
Traceback (most recent call last):
File "/var/folders/2b/dgy6vs4n3lbfy2xqwc3gqq9m0000gn/T/RtmpTqIR6P/chunk-code-108b44104ec28.txt", line 1, in <module> r.x NameError: name 'r' is not defined
Cell 6 (attempt to access previous Python variable in subsequent Python cell):
{python}
x
Output:
Traceback (most recent call last):
File "/var/folders/2b/dgy6vs4n3lbfy2xqwc3gqq9m0000gn/T/RtmpTqIR6P/chunk-code-108b44520d158.txt", line 1, in <module> x NameError: name 'x' is not defined
Any help or advice would be much-appreciated! I have already attempted pointing reticulate at different Conda environments and Python installations with no luck. Thanks!
I think I've figured this out. I misunderstood the reticulate documentation, taking it to mean I could share state between Python cells interactively in RStudio. After perusing the open issues on Github, it appears RStudio integration is still being worked on. When employing knitr directly to knit a document, I get the expected behavior with shared state between cells.
This is fixed in current RStudio e.g. 1.2.1114. But if you are like me stuck with RStudio Server Pro 1.1.456 a workaround is to use reticulate::repl_python() to run python chunks by copy pasting them into a python console. You can close and open the console again if you need to run an R chunk in between - the state will be maintained. When you are done hacking around you can knit the whole file without problems.
The first time I run this block of code from Notebook it works fine:
#Which letters and how many
letters = ["a","b","c"]
noOfLetters = len(letters)
#Looking for all permutations
resultA = []
from itertools import permutations
for i in range(noOfLetters):
resultA.append(list(permutations(letters,i+1)))
If I run it again (without restarting the Kernel) I get the following error:
TypeError Traceback (most recent call last)
<ipython-input-5-4050a4ce7a36> in <module>()
7 from itertools import permutations
8 for i in range(noOfLetters):
----> 9 resultA.append(list(permutations(letters,i+1)))
TypeError: 'list' object is not callable
Assuming "notebook" is Jupyter (previously ipython notebooks), you must be careful that jupyter keeps the state of all variables.
--> that means that the second run starts with variables already initialized at the value they had at the end of the first run.
One way to avoid that is to restart the kernel; another is to delete all variables; one more is to initialize all your variables each time you run.
from the docs:
To restart the kernel (i.e. the computational engine), click on the menu Kernel -> Restart. This can be useful to start over a computation from scratch (e.g. variables are deleted, open files are closed, etc...).