Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constraints function gives AttributeError if targets is in ndarray #513

Open
ysBach opened this issue Oct 31, 2021 · 2 comments
Open

constraints function gives AttributeError if targets is in ndarray #513

ysBach opened this issue Oct 31, 2021 · 2 comments

Comments

@ysBach
Copy link

ysBach commented Oct 31, 2021

For some reason, I have targets in np.ndarray. (I have to mask some targets frequently by comparing them (SkyCoord objects) with a catalog table)

The following code shows the problem:

import astroplan as ap
import numpy as np
from astropy.coordinates import EarthLocation, SkyCoord
from astropy.time import Time

consts = [ap.AtNightConstraint()]
observer = ap.Observer(EarthLocation(lat=0, lon=0, height=0))
targets = np.array([SkyCoord(ra=0, dec=0, unit='deg')])
times = Time("2020-01-01")

ap.is_observable(constraints=consts, observer=observer, targets=targets.tolist(), times=times)
# Works fine, giving [True]

ap.is_observable(constraints=consts, observer=observer, targets=targets, times=times)
# AttributeError: 'numpy.ndarray' object has no attribute 'isscalar'

A simple update to the source code may fix it

# original from https://github.com/astropy/astroplan/blob/master/astroplan/constraints.py#L265
            if targets.isscalar:
# Change to
            if hasattr(targets, "isscalar") and targets.isscalar:

How do you think?

@bmorris3
Copy link
Contributor

bmorris3 commented Nov 3, 2021

Hi @ysBach,

Thanks for this note. In general we recommend that you use target lists as pure Python lists to prevent confusion. Creating object arrays will work for masking, but you can achieve a similar effect with the following syntax without numpy:

masked_targets = [target for target, m in zip(targets, mask) if m]

Let me know if this helps, or if I'm not understanding the problem,
Brett

@bmorris3
Copy link
Contributor

Hi @ysBach, any updates on this? If we should really consider supporting ndarrays of FixedTarget objects then I'm happy to investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants