Skip to content

Commit dd95943

Browse files
committed
Begin building functions to calculate bounding box
1 parent b11a52a commit dd95943

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎astropy/modeling/utils.py‎

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,14 @@ def make_binary_operator_eval(oper, f, g):
361361
"""
362362

363363
return lambda inputs, params: \
364-
tuple(oper(x, y) for x, y in zip(f(inputs, params),
365-
g(inputs, params)))
364+
tuple(oper(x, y) for x, y in zip(f(inputs, params),
365+
g(inputs, params)))
366366

367367

368368
class IncompatibleShapeError(ValueError):
369369
def __init__(self, shape_a, shape_a_idx, shape_b, shape_b_idx):
370370
super(IncompatibleShapeError, self).__init__(
371-
shape_a, shape_a_idx, shape_b, shape_b_idx)
371+
shape_a, shape_a_idx, shape_b, shape_b_idx)
372372

373373

374374
def check_broadcast(*shapes):
@@ -484,3 +484,54 @@ def combine_labels(left, right):
484484

485485
return left + right
486486

487+
488+
def calc_flux_bounds_2D(model, frac=0.99, pos_key='x_pos', y_key='y_pos'):
489+
"""
490+
Calculate the bounding rectangular around a given model that contains some
491+
fraction of the flux.
492+
493+
model should have default attributes bounded = True/False, extent = (x1,x2,y1,y2)
494+
or (x_width,y_width)
495+
"""
496+
497+
try:
498+
from scipy.integrate import dblquad
499+
from scipy.optimize import minimize
500+
except:
501+
raise ImportError
502+
503+
# Get position. Could have different names. Try known variants, throw an error if
504+
# not reconized
505+
for pos_key in ('mean', 'pos', '0'):
506+
try:
507+
x_pos = model.parameters[model.param_names.index('x_' + pos_key)]
508+
y_pos = model.parameters[model.param_names.index('y_' + pos_key)]
509+
break
510+
except:
511+
pass
512+
513+
try:
514+
x_pos, y_pos
515+
except:
516+
raise InputParameterError, 'position keywords not found.'
517+
518+
flux = dblquad(model, -np.inf, np.inf, lambda y: -np.inf, lambda y: np.inf)
519+
520+
# Minimize area in box around the source position
521+
# need to change this somehow to require the minimum containment fraction
522+
def sum_model_2d(x1, x2, y1, y2):
523+
flux_sum = dblquad(x1, x2, lambda y: y1, lambda y: y2)
524+
return np.abs(flux_sum / flux - frac)
525+
526+
result = minimize(sum_fun, model.extent)
527+
528+
return model # other helpful outputs ...
529+
530+
531+
def calc_flux_bounds_1D(model, frac=0.99, x_key='x_pos'):
532+
"""
533+
Calculate the bounding limits around the given model that contains some
534+
fraction of the flux.
535+
536+
model should have default attributes bounded = True/False, extent = (x1,x2)
537+
"""

0 commit comments

Comments
 (0)