pycharm ignores command "integ()" from numpy.polynomial import Polynomialfunction / on jupyter it works - numpy

When using pycharm the integ() function is ignored:
from numpy.polynomial import Polynomial as P
p = P([1, 2, 3])
p.integ()
print(p)
outcome: 1.0 + 2.0 x**1 + 3.0 x**2 (no errors)
on jupyter
it gives me the correct result: 𝑥↦0.0+1.0𝑥+1.0𝑥2+1.0𝑥3
but I really prefer writing code on pycharm - can anyone tell me why this happens or how I could change it??

First, note that p.integ() doesn't change p. It returns a new polynomial object. When you execute print(p) after this expression, you are printing the original p that was created earlier.
In an interactive shell, with a line such as p.integ() that contains an expression (with no assignment), the shell (i.e. Jupyter) prints the value of the expression in the terminal. This is a feature of Jupyter, not of the Python interpreter. When such an expression is encountered in a program, the Python interpreter evaluates the expression, but does not print it. If you want to print the integral of p, you can do something like
q = p.integ()
print(q)

Related

PGF / LaTeX Backend in Matplotlib via Jupyter Notebook SLURM Job on HPC System

I am a university student using my university's computing cluster.
I installed Tex Live to my home directory at ~/.local/texlive/. I have a file called mplrc. The MATPLOTLIBRC environment variable is set to the mplrc file. The mplrc file contains the following lines
backend: pgf
pgf.rcfonts: false
pgf.texsystem: pdflatex
pgf.preamble: \input{mpl_settings.tex}
text.usetex: true
font.family: serif
font.size: 12
The mpl_settings.tex file is in the same directory as the mplrc file and contains the following
\usepackage{amsmath}
\usepackage[T1]{fontenc}
\usepackage{gensymb}
\usepackage{lmodern}
\usepackage{siunitx}
On the cluster I am using, I must submit a SLURM job to run the Jupyter notebook. The example code I am trying to run within the notebook is
formula = (
r'$\displaystyle '
r'N = \int_{E_\text{min}}^{E_\text{max}} '
r'\int_0^A'
r'\int_{t_\text{min}}^{t_\text{max}} '
r'\Phi_0 \left(\frac{E}{\SI{1}{\GeV}}\right)^{\!\!-γ}'
r' \, \symup{d}A \, \symup{d}t \, \symup{d}E'
r'$'
)
def power_law_spectrum(energy, normalisation, spectral_index):
return normalisation * energy**(-spectral_index)
bin_edges = np.logspace(2, 5, 15)
bin_centers = 0.5 * (bin_edges[:-1] + bin_edges[1:])
y = power_law_spectrum(bin_centers, 1e-5, 2.5)
relative_error = np.random.normal(1, 0.2, size=len(y))
y_with_err = relative_error * y
fig, ax = plt.subplots()
ax.errorbar(
np.log10(bin_centers),
y_with_err,
xerr=[
np.log10(bin_centers) - np.log10(bin_edges[:-1]),
np.log10(bin_edges[1:]) - np.log10(bin_centers)
],
yerr=0.5 * y_with_err,
linestyle='',
)
ax.text(0.1, 0.1, formula, transform=plt.gca().transAxes)
ax.set_yscale('log')
fig.tight_layout(pad=0)
plt.show()
This generates an enormous error message, but the root of it is
RuntimeError: latex was not able to process the following string:
b'lp'
However, underneath that, I see what I think is the real problem
! LaTeX Error: File `article.cls' not found.
I've set my PATH so that it finds the right latex command, but what else needs to be set in order to find the article.cls file? It seems like it's something particular to the Python notebook. When running kpsewhich article.cls in a terminal within the Jupyterlab interface, the file gets found. But trying ! kpsewhich article.cls or subprocess.run(['kpsewhich', 'article.cls']) within the Python notebook does not find the file.
I figured it out. I forgot I had run a section of code which set
TEXINPUTS=/path/to/some/directory
Looks like I missed a : in my TEXINPUTS, so TeX was only looking in /path/to/some/directory
The solution was to have
TEXINPUTS=/path/to/some/directory:
That way it looked in my current directory, but also continued looking elsewhere.

why would a line of code work when evaluated, but not when run

I have passed a DataFrame from an ipython notebook to a function inside a standard .py file.
In the function, i'm using df['column_name'].values to try to extract all the values from that column.
While debugging (in pycharm), I am running this line in the 'evaluate' tool pycharm provides and it works fine. However, when I run the same line normally (outside the tool window), i get an error:
"TypeError: list indices must be integers or slices, not str"
When looking at my dataframe in the workspace variables section, it is interpreted correctly as a dataframe.
The dataframe object I am passing has 3 columns ('x','y' and 'likelyhood'), each containing integers/floats. The line in question that unpacks the values inside one of these columns is the first line of the function.
Can anyone explain how this can happen? What is the difference that causes the fact that a line of code can work in one, and return an error in the other?
and also what might cause this specific TypeError and how can I solve this bug?
the data frame as printed by df.tail(10):
coords x y likelihood
105570 297.497525 332.355521 1.0
105571 297.463208 332.353797 1.0
105572 297.439774 332.383908 1.0
105573 297.457581 332.458205 1.0
105574 297.487260 332.402202 1.0
105575 297.519772 332.451551 1.0
105576 297.495998 332.431064 1.0
105577 297.516722 332.113481 1.0
105578 297.542539 332.080923 1.0
105579 297.528317 332.046282 1.0
full function:
import math
import numpy as np
import pandas as pd
def filter_jumps(bp, thresh=10):
"""
the function watches for jumps that cannot happen, and interpolates the location of the point acoordingly
"""
x=bp['x'].values
y=bp['y'].values
for i in range(1,len(x)):
if np.abs(x[i]+y[i]-x[i-1]-y[i-1]) > thresh:
start = i-1; end = None; idx=i+1
while not end:
if np.abs(x[idx]+y[idx]-x[idx-1]-y[idx-1]) > thresh:
end = idx
else:
idx += 1
rang = end-start
x[start:end] = np.linspace(x[start], x[end], rang)
y[start:end] = np.linspace(y[start], y[end], rang)
bp['x'] = x
bp['y'] = y
return bp

Plotting Method error using PyPlot in IJulia

I am trying to plot a function using PyPlot on an IJulia notebook, but I keep obtaining error messages.
When I ran this code:
function gtest2(x)
6.34*(log2(1+exp(10.0*(x+0.5))))^0.8
end
using PyPlot
x = -1.0:0.1:1.0;
plot(x, gtest2(x));
I got errors like these:
MethodError: no method matching ^(::Array{Float64,1}, ::Float64)
Closest candidates are: ^(::Float64, ::Float64) at math.jl:355 ...
I tried to defined a different type of variable while defining my function using gtest2(x::Number) or gtest2(x::Float64) but I have the same errors.
It does the same using linespace instead of -1.0:0.1:1.0. I understand that the format the function sees in input does not match the definition but I don't get what I'm doing wrong because simple functions work:
function f(x)
x
end
plot(x,f(x))
Why am I getting those errors in the first case?
I am using IJulia notebook 0.5.1 on safari.
Your code doesn't properly handle vectors thus you need to either change gtest
using the . vectorization syntax
function gtest2(x)
6.34*(log2.(1 + exp.(10.0*(x + 0.5)))).^0.8
end
or even easier use the dot vectorization as follows
plot(x, gtest2.(x));
To learn more about dot vectorization please see the following in the docs: https://docs.julialang.org/en/latest/manual/functions.html#man-vectorized-1
The first definition works also with:
map(gtest2, x)
or
gtest2.(x)

tensorflow tf.Print not printing anything in Jupyter

Trying debug statements in Python/tensorflow1.0 using jupyter , but does not get any output printed from tf.Print
Thought sess.run(during training in below code) should have evaluated db1 tensor and print output which did not happen
However db1.eval in evaluate phase , printing entire tensor X with out "message X:".
def combine_inputs(X):
db1=tf.Print(X,[X],message='X:')
return (tf.matmul(X, W) + b,db1)
<<training code>>
_,summary=sess.run([train_op,merged_summaries])
## merged_summaries tensor triggers combine_inputs function. There are
## other tensor functions/coding in between , not giving entire code to keep
## it simple; code works as expected except tf.Print
<<evaluate code>>
print(db1.eval())
Confused on following
a) Why tf.Print is not printing during sess.run during training?
b) Why explicit db1.eval is necessary , expected tf.Print to trigger with
sess.run. If eval is required , could copy tensor X in my code to db1
and evaluate it with out tf.Print. Correct?
Tried going through other questions (like below one). Suggested to implement memory_util or predefined function. As learner could not understand why tf.Print does not work in my scenario
If anyone encountered similar issues , please assist. Thanks!
Similar question in stackoverflow
According to the documentation, tf.Print prints to standard error (as of version 1.1), and it's not compatible with jupyter notebook. That's why you can't see any output.
Check here:
https://www.tensorflow.org/api_docs/python/tf/Print
You can check the terminal where you launched the jupyter notebook to see the message.
import tensorflow as tf
tf.InteractiveSession()
a = tf.constant(1)
b = tf.constant(2)
opt = a + b
opt = tf.Print(opt, [opt], message="1 + 2 = ")
opt.eval()
In the terminal, I can see:
2018-01-02 23:38:07.691808: I tensorflow/core/kernels/logging_ops.cc:79] 1 + 2 = [3]

Not contract X*X*X to pow(X,3) in sympy's `printing.ccode` method

I have a sympy equation that I need to translate to CUDA.
In its default configuration, sympy.printing.ccode will transform the expression x*x into pow(x,2) which unfortunately, CUDA behaves a bit strangely with (e.g. pow(0.1,2) is 0 according to CUDA).
I would prefer sympy.printing.ccode to leave these kinds of expressions unaltered, or put another way, I would like it to expand any instance of pow into a simple product. E.g. pow(x,4) would become x*x*x*x -- does anyone know how to make this happen?
This should do it:
>>> import sympy as sp
>>> from sympy.utilities.codegen import CCodePrinter
>>> print(sp.__version__)
0.7.6.1
>>> x = sp.Symbol('x')
>>> CCodePrinter().doprint(x*x*x)
'pow(x, 3)'
>>> class MyCCodePrinter(CCodePrinter):
... def _print_Pow(self, expr):
... if expr.exp.is_integer and expr.exp.is_number:
... return '(' + '*'.join([self._print(expr.base)]*expr.exp) + ')'
... else:
... return super(MyCCodePrinter, self)._print_Pow(expr)
...
>>> MyCCodePrinter().doprint(x*x*x)
'x*x*x'
Note that this was a proposed change (with a restriction on this size of the exponent) for a while. Then the motivation was performance of regular C code, but flags such as -ffast-math made to point moot. However if this is something that is useful for CUDA code we should definitely support the behaviour through a setting, feel free to open an issue for it if you think it is needed.