NameError: name ‘log’ is not defined
Issues with numpy imports in:
- Tutorial (https://graph-tool.skewed.de/static/doc/demos/reconstruction_indirect/reconstruction.html)
-
get_precision()
method inPseudoNormalBlockState
The first issue can be reproduced by simply running the code from the tutorial:
import graph_tool.all as gt
import numpy as np
import requests
from collections import defaultdict
import csv
r = requests.get('https://downloads.skewed.de/data/all_stocks_5yr.csv')
lines = r.iter_lines(decode_unicode=True)
next(lines) # remove header
prices = defaultdict(list)
for vals in csv.reader(lines, delimiter=','):
prices[vals[-1]].append(float(vals[4])) # use closing price
stocks, s = zip(*[(stock, vals) for stock, vals in prices.items() if len(vals) == 1259])
# compute log-returns
s = [[log(p[i+1]) - log(p[i]) for i in range(len(p)-1)] for p in s
This produces "NameError: name 'log' is not defined". I can, of course, fix this is in the code by myself (or do a wildcard import from numpy), but I think it should be adressed in the documentation.
Fixing this manually and continuing with graph inference, a similar error arises when trying to invoke get_precision()
method of the resulting object:
import graph_tool.all as gt
import numpy as np
import requests
from collections import defaultdict
import csv
r = requests.get('https://downloads.skewed.de/data/all_stocks_5yr.csv')
lines = r.iter_lines(decode_unicode=True)
next(lines) # remove header
prices = defaultdict(list)
for vals in csv.reader(lines, delimiter=','):
prices[vals[-1]].append(float(vals[4])) # use closing price
stocks, s = zip(*[(stock, vals) for stock, vals in prices.items() if len(vals) == 1259])
# compute log-returns
s = [[np.log(p[i+1]) - np.log(p[i]) for i in range(len(p)-1)] for p in s
s = np.array(s)
state = gt.PseudoNormalBlockState(s)
delta = np.inf
while delta > 20:
ret = state.mcmc_sweep(beta=np.inf, niter=1)
delta = abs(ret[0])
p = state.get_precision()
This also results in "NameError: name 'log' is not defined". Looking at the code of the function (https://git.skewed.de/count0/graph-tool/-/blob/master/src/graph_tool/inference/reconstruction.py), I see that there is a similar issue with 1/exp(theta[v] * 2)
, line 2213.