Skip to content

Commit

Permalink
custom asfarray funtion
Browse files Browse the repository at this point in the history
  • Loading branch information
iparask committed Mar 4, 2025
1 parent 3c4655c commit 0a57276
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pixell/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def getsys_full(sys, time=None, site=default_site, bore=None):
r = toks[0]
refsys = getsys(toks[1]) if len(toks) > 1 else prevsys
try:
r = np.asarray(r.split("_"), dtype=np.float64)*utils.degree
r = utils.asfarray(r.split("_"))*utils.degree

Check warning on line 374 in pixell/coordinates.py

View check run for this annotation

Codecov / codecov/patch

pixell/coordinates.py#L374

Added line #L374 was not covered by tests
assert(r.ndim == 1 and len(r) == 2)
r = transform_raw(refsys, base, r[:,None], time=time, site=site, bore=bore)
except ValueError:
Expand Down
2 changes: 1 addition & 1 deletion pixell/enmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ def extent_subgrid(shape, wcs, nsub=None, safe=True, signed=False):
if nsub is None: nsub = 17
# Create a new wcs with (nsub,nsub) pixels
wcs = wcs.deepcopy()
step = (np.asarray(shape[-2:], dtype=np.float64)/nsub)[::-1]
step = (utils.asfarray(shape[-2:])/nsub)[::-1]

Check warning on line 902 in pixell/enmap.py

View check run for this annotation

Codecov / codecov/patch

pixell/enmap.py#L902

Added line #L902 was not covered by tests
wcs.wcs.crpix -= 0.5
wcs.wcs.cdelt *= step
wcs.wcs.crpix /= step
Expand Down
8 changes: 4 additions & 4 deletions pixell/interpol.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def build(func, interpolator, box, errlim, maxsize=None, maxtime=None, return_ob
automatically polls func and constructs an interpolator
object that has the required accuracy inside the provided
bounding box."""
box = np.asarray(box, dtype=np.float64)
errlim = np.asarray(errlim, dtype=np.float64)
box = utils.asfarray(box)
errlim = utils.asfarray(errlim)

Check warning on line 132 in pixell/interpol.py

View check run for this annotation

Codecov / codecov/patch

pixell/interpol.py#L131-L132

Added lines #L131 - L132 were not covered by tests
idim = box.shape[1]
n = [3]*idim if nstart is None else nstart
n = np.array(n) # starting mesh size
Expand Down Expand Up @@ -265,7 +265,7 @@ def lin_derivs_forward(y, npre=0):
dimensions, returning an array of shape (2,)*n+(:,)*npre+(:-1,)*n. That is,
it is one shorter in each direction along which the derivative is taken.
Derivatives are computed using forward difference."""
y = np.asarray(y, dtype=np.float64)
y = utils.asfarray(y)

Check warning on line 268 in pixell/interpol.py

View check run for this annotation

Codecov / codecov/patch

pixell/interpol.py#L268

Added line #L268 was not covered by tests
nin = y.ndim-npre
ys = np.zeros((2,)*nin+y.shape)
ys[(0,)*nin] = y
Expand All @@ -283,7 +283,7 @@ def grad_forward(y, npre=0):
"""Given an array y with npre leading dimensions and n following dimensions,
the gradient along the n last dimensions, returning an array of shape (n,)+y.shape.
Derivatives are computed using forward difference."""
y = np.asarray(y, dtype=np.float64)
y = utils.asfarray(y)

Check warning on line 286 in pixell/interpol.py

View check run for this annotation

Codecov / codecov/patch

pixell/interpol.py#L286

Added line #L286 was not covered by tests
nin = y.ndim-npre
dy = np.zeros((nin,)+y.shape)
for i in range(nin):
Expand Down
2 changes: 1 addition & 1 deletion pixell/reproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def rot2euler(rot):
else: raise ValueError("Unrecognized system '%s'" % osys)
return R.as_euler("zyz")
else:
rot = np.asarray(rot, dtype=np.float64)
rot = utils.asfarray(rot)

Check warning on line 383 in pixell/reproject.py

View check run for this annotation

Codecov / codecov/patch

pixell/reproject.py#L383

Added line #L383 was not covered by tests
return rot

def inv_euler(euler): return [-euler[2], -euler[1], -euler[0]]
Expand Down
36 changes: 21 additions & 15 deletions pixell/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ def contains(array, vals):
inds[inds>=len(vals)] = 0
return vals[inds] == array

def asfarray(arr, default_dtype=np.float64):
arr = np.asanyarray(arr)
if not np.issubdtype(arr.dtype, np.floating) and not np.issubdtype(arr.dtype, np.complexfloating):
arr = arr.astype(default_dtype)
return arr

Check warning on line 158 in pixell/utils.py

View check run for this annotation

Codecov / codecov/patch

pixell/utils.py#L155-L158

Added lines #L155 - L158 were not covered by tests

def common_vals(arrs):
"""Given a list of arrays, returns their intersection.
For example
Expand Down Expand Up @@ -394,7 +400,7 @@ def weighted_quantile(map, ivar, quantile, axis=-1):
and quantile has shape B, then the result will have shape B+A.
"""
map, ivar = np.broadcast_arrays(map, ivar)
quantile = np.asarray(quantile, dtype=np.float64)
quantile = asfarray(quantile)

Check warning on line 403 in pixell/utils.py

View check run for this annotation

Codecov / codecov/patch

pixell/utils.py#L403

Added line #L403 was not covered by tests
axis = axis % map.ndim
# Move axis to end
map = np.moveaxis(map, axis, -1)
Expand All @@ -408,8 +414,8 @@ def weighted_quantile(map, ivar, quantile, axis=-1):
quantile = quantile.reshape(-1) # [B]
# Sort
order = np.argsort(map, -1)
map = np.asarray(np.take_along_axis(map, order, -1), dtype=np.float64)
ivar = np.asarray(np.take_along_axis(ivar, order, -1), dtype=np.float64)
map = asfarray(np.take_along_axis(map, order, -1))
ivar = asfarray(np.take_along_axis(ivar, order, -1))

Check warning on line 418 in pixell/utils.py

View check run for this annotation

Codecov / codecov/patch

pixell/utils.py#L417-L418

Added lines #L417 - L418 were not covered by tests
# We don't have interp or searchsorted for this case, so do it ourselves.
# The 0.5 part corresponds to the C=0.5 case in the method
icum = np.cumsum(ivar, axis=-1) # [A,n]
Expand Down Expand Up @@ -717,7 +723,7 @@ def grid(box, shape, endpoint=True, axis=0, flat=False):
grid([[0],[1]],[4]) => [[0,0000, 0.3333, 0.6667, 1.0000]]
"""
n = np.asarray(shape)
box = np.asarray(box, dtype=np.float64)
box = asfarray(box)

Check warning on line 726 in pixell/utils.py

View check run for this annotation

Codecov / codecov/patch

pixell/utils.py#L726

Added line #L726 was not covered by tests
off = -1 if endpoint else 0
inds = np.rollaxis(np.indices(n),0,len(n)+1) # (d1,d2,d3,...,indim)
res = inds * (box[1]-box[0])/(n+off) + box[0]
Expand Down Expand Up @@ -895,9 +901,9 @@ def range_sub(a,b, mapping=False):
Example: utils.range_sub([[0,100],[200,1000]], [[1,2],[3,4],[8,999]], mapping=True)
(array([[ 0, 1],
[ 2, 3],
[ 4, 8],
[ 999, 1000]]),
[ 2, 3],
[ 4, 8],
[ 999, 1000]]),
array([0, 0, 0, 1]),
array([ 0, -1, 1, -2, 2, -3, 3]))
Expand All @@ -910,9 +916,9 @@ def range_sub(a,b, mapping=False):
The same call without mapping: utils.range_sub([[0,100],[200,1000]], [[1,2],[3,4],[8,999]])
array([[ 0, 1],
[ 2, 3],
[ 4, 8],
[ 999, 1000]])
[ 2, 3],
[ 4, 8],
[ 999, 1000]])
"""
def fixshape(a):
a = np.asarray(a)
Expand Down Expand Up @@ -1168,10 +1174,10 @@ def greedy_split(data, n=2, costfun=max, workfun=lambda w,x: x if w is None else
for each group score = scorefun([workval,workval,....]).
Example: greedy_split(range(10)) => [[9,6,5,2,1,0],[8,7,4,3]]
greedy_split([1,10,100]) => [[2],[1,0]]
greedy_split("012345",costfun=lambda x:sum([xi**2 for xi in x]),
workfun=lambda w,x:0 if x is None else int(x)+w)
=> [[5,2,1,0],[4,3]]
greedy_split([1,10,100]) => [[2],[1,0]]
greedy_split("012345",costfun=lambda x:sum([xi**2 for xi in x]),
workfun=lambda w,x:0 if x is None else int(x)+w)
=> [[5,2,1,0],[4,3]]
"""
# Sort data based on standalone costs
costs = []
Expand Down Expand Up @@ -2408,7 +2414,7 @@ def tsz_profile_los(x, xc=0.497, alpha=1.0, beta=-4.65, gamma=-0.3, zmax=1e5, np
_tsz_profile_los_cache[key] = (interpolate.interp1d(xp, yp, "cubic"), x1, x2, yp[0], yp[-1], (yp[-2]-yp[-1])/(xp[-2]-xp[-1]))
spline, xmin, xmax, vleft, vright, slope = _tsz_profile_los_cache[key]
# Split into 3 cases: x<xmin, x inside and x > xmax.
x = np.asarray(x, dtype=np.float64)
x = asfarray(x)

Check warning on line 2417 in pixell/utils.py

View check run for this annotation

Codecov / codecov/patch

pixell/utils.py#L2417

Added line #L2417 was not covered by tests
left = x<xmin
right = x>xmax
inside= (~left) & (~right)
Expand Down

0 comments on commit 0a57276

Please sign in to comment.