cta-observatory / pyirf

Python IRF builder
https://pyirf.readthedocs.io/en/stable/
MIT License
15 stars 25 forks source link

effective_area_per_energy_and_fov() creates error in version 0.10.0 #261

Closed tristangrad closed 12 months ago

tristangrad commented 12 months ago

using this function with below specified (but probably arbitrarily) args creates the error 'AttributeError: 'numpy.ndarray' object has no attribute '_set_unit''

minimal (not) working example:

simulation_info = SimulatedEventsInfo(
    energy_min=0.003*u.TeV,
    energy_max=330*u.TeV,
    spectral_index=-2,
    n_showers=1000000,
    max_impact=500 * u.m,
    viewcone_min=0 * u.deg,
    viewcone_max=10 * u.deg,
)

true_energy_bins= arbitrary 
fov_offset_bins= arbitrary
selected_events = QTable(data=[[0.05*u.TeV] , [1.35*u.deg]],names=['true_energy','true_source_fov_offset'])
StFroese commented 12 months ago

The code for reproducing the error is missing, right? This is not failing for me

StFroese commented 12 months ago

ahh the function is in the title...

StFroese commented 12 months ago

works for me:

In [24]: fov_offset_bins = u.Quantity(sorted(np.random.rand(100)), 'deg')

In [25]: true_energy_bins = u.Quantity(sorted(np.random.rand(100)), 'TeV')

In [26]: 

In [26]: effective_area_per_energy_and_fov(selected_events, simulation_info, true_energy_bins, fov_offset_bins)
Out[26]: 
<Quantity [[0., 0., 0., ..., 0., 0., 0.],
           [0., 0., 0., ..., 0., 0., 0.],
           [0., 0., 0., ..., 0., 0., 0.],
           ...,
           [0., 0., 0., ..., 0., 0., 0.],
           [0., 0., 0., ..., 0., 0., 0.],
           [0., 0., 0., ..., 0., 0., 0.]] m2>
RuneDominik commented 12 months ago

Same here, I've rewritten your example as a test, which passes. Maybe some conflicting versions of astropy and numpy?


def test_viewcone_integral_effective_area_per_energy_and_fov():
    import astropy.units as u

    from astropy.table import QTable

    from pyirf.irf import effective_area_per_energy_and_fov
    from pyirf.simulations import SimulatedEventsInfo

    simulation_info = SimulatedEventsInfo(
        energy_min=0.003 * u.TeV,
        energy_max=330 * u.TeV,
        spectral_index=-2,
        n_showers=1000000,
        max_impact=500 * u.m,
        viewcone_min=0 * u.deg,
        viewcone_max=10 * u.deg,
    )

    fov_bins = [0, 2] * u.deg
    energy_bins = [0, 1] * u.TeV

    selected_events = QTable(
        data=[[0.05 * u.TeV], [1.35 * u.deg]],
        names=["true_energy", "true_source_fov_offset"],
    )

    aeff = effective_area_per_energy_and_fov(
        selected_events=selected_events,
        simulation_info=simulation_info,
        true_energy_bins=energy_bins,
        fov_offset_bins=fov_bins,
    )

    assert aeff >= 0

Tested with the current pyirf main and astropy=5.3.2 numpy=1.25.2 python=3.11.5

Edit: Also works in a fresh python 3.9 env with pyirf v0.10

StFroese commented 12 months ago

Test works for me too with: astropy=5.3.3 numpy=1.25.2 python=3.9.18

installed via conda