Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruno46 committed Feb 4, 2022
1 parent 259884e commit 6bece0c
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pyobs/core/cdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, cov, mask):
for a in self.mask:
ia = self.mask.index(a)
# special case cobs_scalar * obs_vector, then mask is vector but n=1
if n==1:
if n == 1:
self.grad[ia, 0] = 1.0
else:
self.grad[ia, a] = 1.0
Expand Down
2 changes: 1 addition & 1 deletion pyobs/core/derobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,6 @@ def error_bias4(x, f):
x.delta[key].delta,
)
dd2 = numpy.sum(d2, axis=1)
bias4 += dd2 ** 2 / x.delta[key].n ** 4
bias4 += dd2**2 / x.delta[key].n ** 4

return numpy.sqrt(bias4)
8 changes: 5 additions & 3 deletions pyobs/core/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def find_opt(self):

def set_opt(self, xopt):
for a in range(self.size):
if isinstance(xopt,(list, numpy.ndarray)):
if isinstance(xopt, (list, numpy.ndarray)):
if xopt[a] in self.x:
i = self.x.index(xopt[a])
else:
Expand All @@ -131,9 +131,11 @@ def correct_gamma_bias(self):
# self.cvar[a, :] += self.cvar[a, :] * f
# Gamma -> Gamma + C/N
# cumsum(Gamma) -> cumsum(Gamma) + cumsum(C/N)
self.cvar[a, :] += numpy.arange(1,len(self.cvar[a,:])+1)*self.var[a,0] / self.N[a]
self.cvar[a, :] += (
numpy.arange(1, len(self.cvar[a, :]) + 1) * self.var[a, 0] / self.N[a]
)
self.set_opt(self.xopt)

def tauint(self):
tau = numpy.zeros((self.full_size, 2))
for a in self.mask:
Expand Down
4 changes: 2 additions & 2 deletions pyobs/core/ndobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def __matmul__(self, y):

def reciprocal(self):
new_mean = numpy.reciprocal(self.mean)
g0 = pyobs.gradient(lambda x: -x * (new_mean ** 2), self.mean, gtype="diag")
g0 = pyobs.gradient(lambda x: -x * (new_mean**2), self.mean, gtype="diag")
return pyobs.derobs([self], new_mean, [g0])

def __truediv__(self, y):
Expand Down Expand Up @@ -538,7 +538,7 @@ def __rmatmul__(self, y):
return pyobs.derobs([self], y @ self.mean, [g0])

def __pow__(self, a):
new_mean = self.mean ** a
new_mean = self.mean**a
g0 = pyobs.gradient(
lambda x: a * x * self.mean ** (a - 1), self.mean, gtype="diag"
)
Expand Down
4 changes: 2 additions & 2 deletions pyobs/misc/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import sys, os, numpy

book = {}
MB = 1024.0 ** 2
GB = 1024.0 ** 3
MB = 1024.0**2
GB = 1024.0**3


def get_size(obj):
Expand Down
44 changes: 24 additions & 20 deletions pyobs/misc/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ def sample_boolean(self, N):
self.state = numpy.random.get_state()
return r


def acrand(self, tau, N, n=1):
r = numpy.reshape(self.sample_normal(N*n), (N,n))
r = numpy.reshape(self.sample_normal(N * n), (N, n))

if tau > 0.5:
f = numpy.exp(-1.0 / tau)
Expand All @@ -70,19 +69,18 @@ def acrand(self, tau, N, n=1):
tau = 0.5
ff = numpy.sqrt(1.0 - f * f)
rn = numpy.zeros(numpy.shape(r))
rn[0,:] = ff * r[0,:]
rn[0, :] = ff * r[0, :]
for i in range(1, N):
rn[i,:] = ff * r[i,:] + f * rn[i - 1,:]
rn[i, :] = ff * r[i, :] + f * rn[i - 1, :]
return rn


def markov_chain(self, mu, cov, taus, N, couplings=None):
"""
Create synthetic autocorrelated Monte Carlo data
Parameters:
mu (float or 1D array): the central value
cov (float or array): the target covariance matrix; if a 1-D array is
cov (float or array): the target covariance matrix; if a 1-D array is
passed, a diagonal covariance matrix is assumed
taus (float or array): the autocorrelation time(s). Values smaller
than 0.5 are ignored and set to automatically to 0.5.
Expand All @@ -103,38 +101,44 @@ def markov_chain(self, mu, cov, taus, N, couplings=None):
"""
mu = pyobs.array(mu)
pyobs.assertion(numpy.ndim(mu)==1, "only 1D arrays are supported")
pyobs.assertion(numpy.ndim(mu) == 1, "only 1D arrays are supported")
na = len(mu)

cov = pyobs.array(cov)
pyobs.assertion(numpy.shape(cov)[0]==na, "covariance matrix does not match central values")

pyobs.assertion(
numpy.shape(cov)[0] == na, "covariance matrix does not match central values"
)

taus = pyobs.array(taus)
taus = 0.5 * (taus <= 0.5) + taus * (taus > 0.5)
nt = len(taus)
if couplings is None:
couplings = pyobs.array(numpy.ones((na,nt)))
couplings = pyobs.array(numpy.ones((na, nt)))
else:
couplings = pyobs.array(couplings)
pyobs.assertion(numpy.shape(couplings)==(na,nt), f"unexpected couplings for {na} values and {nt} modes")

rn = numpy.zeros((N,na))
_c = numpy.stack([couplings]*N)
pyobs.assertion(
numpy.shape(couplings) == (na, nt),
f"unexpected couplings for {na} values and {nt} modes",
)

rn = numpy.zeros((N, na))
_c = numpy.stack([couplings] * N)
for i in range(len(taus)):
rn += _c[:,:,i] * self.acrand(taus[i], N, na)
rn += _c[:, :, i] * self.acrand(taus[i], N, na)

pref = numpy.sqrt(N / (2 * (couplings**2 @ taus)))

if numpy.ndim(cov) == 1:
Q = numpy.diag(numpy.sqrt(cov))
else:
[w, v] = numpy.linalg.eig(cov)
Q = numpy.diag(numpy.sqrt(w)) @ v.T
if na==1:

if na == 1:
return (mu + (pref * rn) @ Q).reshape((N,))
return mu + (pref * rn) @ Q


# def acrandn(self, mu, cov, tau, N):
# """
# Create synthetic correlated Monte Carlo 1-D data
Expand Down
2 changes: 1 addition & 1 deletion pyobs/optimize/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ def __call__(self, x):
x = numpy.array(x)
res = pyobs.repeat(self.coeff[0], N)
for i in range(1, self.k):
res += pyobs.repeat(self.coeff[i], N) * (x ** i)
res += pyobs.repeat(self.coeff[i], N) * (x**i)
return res
2 changes: 1 addition & 1 deletion pyobs/qft/free_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

def Gt(t, m, p, L):
phat = 2 * numpy.sin(numpy.array(p) * numpy.pi / numpy.array(L))
om = numpy.arccosh(1.0 + 0.5 * (m ** 2 + numpy.sum(phat ** 2)))
om = numpy.arccosh(1.0 + 0.5 * (m**2 + numpy.sum(phat**2)))
return numpy.exp(-numpy.abs(t) * om) * 0.5 / numpy.sinh(om)


Expand Down
2 changes: 1 addition & 1 deletion pyobs/tensor/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,4 @@ def matrix_power(x, a):
>>> matsqrt @ mat @ matsqrt # return the identity
"""
[w, v] = eig(x)
return v @ pyobs.diag(w ** a) @ pyobs.transpose(v)
return v @ pyobs.diag(w**a) @ pyobs.transpose(v)
4 changes: 2 additions & 2 deletions pyobs/tensor/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def arctan(x):
Examples:
>>> arctanA = pyobs.arctan(obsA)
"""
return __unary(x, numpy.arctan, lambda x: 1 / (1 + x ** 2))
return __unary(x, numpy.arctan, lambda x: 1 / (1 + x**2))


def cosh(x):
Expand Down Expand Up @@ -287,7 +287,7 @@ def arccosh(x):
Examples:
>>> B = pyobs.arccosh(obsA)
"""
return __unary(x, numpy.arccosh, lambda x: 1.0 / numpy.sqrt(x ** 2 - 1))
return __unary(x, numpy.arccosh, lambda x: 1.0 / numpy.sqrt(x**2 - 1))


# new_mean = numpy.arccosh(x.mean)
Expand Down
5 changes: 3 additions & 2 deletions pyobs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def tex_table(mat, fmt=None):
else:
h += [f"%{fmt[i]}" % mat[a, i]]
i += 1
outstr += [fr'{" & ".join(h)} \\ ']
outstr += [rf'{" & ".join(h)} \\ ']

return outstr

Expand Down Expand Up @@ -255,7 +255,8 @@ def core(string):
return numpy.array(out)
return core(data)


def array(x):
if numpy.shape(x)==():
if numpy.shape(x) == ():
return numpy.array(x).reshape((1,))
return numpy.array(x)

0 comments on commit 6bece0c

Please sign in to comment.