Skip to content

The behaviour of CompoundModel.evaluate and CompoundModel.__call__ are different #16675

@Cadair

Description

@Cadair

Description

I don't know why __call__ doesn't use .evaluate but it doesn't. Having two code paths apparently means they can do different things.

Expected behavior

No response

How to Reproduce

import numpy as np
from astropy.modeling.models import Linear1D
import astropy.units as u

x = np.linspace(0, 1, 101) * u.s
y = np.linspace(5, 10, 101) * u.m * u.m * u.kg / u.s

m1 = Linear1D(slope=5 * u.m / u.s / u.s, intercept=1.0 * u.m / u.s)
m2 = Linear1D(slope=0.0 * u.kg / u.s, intercept=10.0 * u.kg)
m3 = Linear1D(slope=0.0 * u.m / u.s, intercept=10.0 * u.m)
truth = m1 * m2 * m3

new_parameter_values = [ 5.,  1.,  0., 10.,  0., 10.0012207]
new_parameter_quantites = [new_parameter_values[i] * getattr(truth, name).unit for i, name in enumerate(truth.param_names)]

print("evaluate with units")
units_eval_result = truth.evaluate(x, *new_parameter_quantites)
print(units_eval_result)

updated_truth = truth.copy()
updated_truth.parameters = new_parameter_values

print("call with units")
units_call_result = updated_truth(x)
print(units_call_result)

print(f"{u.allclose(units_eval_result, units_call_result)=}")

unitless = truth.without_units_for_data(x=x, y=y)[0]

print("evaluate without units")
eval_result = unitless.evaluate(x.value, *new_parameter_values)
print(eval_result)

updated_unitless = unitless.copy()
updated_unitless.parameters = new_parameter_values

print("call without units")
call_result = updated_unitless(x.value)
print(call_result)

print(f"{u.allclose(eval_result, call_result)=}")

which outputs:

evaluate with units
[100.012207   105.01281735 110.0134277  115.01403805 120.0146484
 125.01525875 130.0158691  135.01647945 140.0170898  145.01770015
 150.0183105  155.01892085 160.0195312  165.02014155 170.0207519
 175.02136225 180.0219726  185.02258295 190.0231933  195.02380365
 200.024414   205.02502435 210.0256347  215.02624505 220.0268554
 225.02746575 230.0280761  235.02868645 240.0292968  245.02990715
 250.0305175  255.03112785 260.0317382  265.03234855 270.0329589
 275.03356925 280.0341796  285.03478995 290.0354003  295.03601065
 300.036621   305.03723135 310.0378417  315.03845205 320.0390624
 325.03967275 330.0402831  335.04089345 340.0415038  345.04211415
 350.0427245  355.04333485 360.0439452  365.04455555 370.0451659
 375.04577625 380.0463866  385.04699695 390.0476073  395.04821765
 400.048828   405.04943835 410.0500487  415.05065905 420.0512694
 425.05187975 430.0524901  435.05310045 440.0537108  445.05432115
 450.0549315  455.05554185 460.0561522  465.05676255 470.0573729
 475.05798325 480.0585936  485.05920395 490.0598143  495.06042465
 500.061035   505.06164535 510.0622557  515.06286605 520.0634764
 525.06408675 530.0646971  535.06530745 540.0659178  545.06652815
 550.0671385  555.06774885 560.0683592  565.06896955 570.0695799
 575.07019025 580.0708006  585.07141095 590.0720213  595.07263165
 600.073242  ] m2 kg / s
call with units
[100.012207   105.01281735 110.0134277  115.01403805 120.0146484
 125.01525875 130.0158691  135.01647945 140.0170898  145.01770015
 150.0183105  155.01892085 160.0195312  165.02014155 170.0207519
 175.02136225 180.0219726  185.02258295 190.0231933  195.02380365
 200.024414   205.02502435 210.0256347  215.02624505 220.0268554
 225.02746575 230.0280761  235.02868645 240.0292968  245.02990715
 250.0305175  255.03112785 260.0317382  265.03234855 270.0329589
 275.03356925 280.0341796  285.03478995 290.0354003  295.03601065
 300.036621   305.03723135 310.0378417  315.03845205 320.0390624
 325.03967275 330.0402831  335.04089345 340.0415038  345.04211415
 350.0427245  355.04333485 360.0439452  365.04455555 370.0451659
 375.04577625 380.0463866  385.04699695 390.0476073  395.04821765
 400.048828   405.04943835 410.0500487  415.05065905 420.0512694
 425.05187975 430.0524901  435.05310045 440.0537108  445.05432115
 450.0549315  455.05554185 460.0561522  465.05676255 470.0573729
 475.05798325 480.0585936  485.05920395 490.0598143  495.06042465
 500.061035   505.06164535 510.0622557  515.06286605 520.0634764
 525.06408675 530.0646971  535.06530745 540.0659178  545.06652815
 550.0671385  555.06774885 560.0683592  565.06896955 570.0695799
 575.07019025 580.0708006  585.07141095 590.0720213  595.07263165
 600.073242  ] m2 kg / s
u.allclose(units_eval_result, units_call_result)=True
evaluate without units
[100.012207   105.01281735 110.0134277  115.01403805 120.0146484
 125.01525875 130.0158691  135.01647945 140.0170898  145.01770015
 150.0183105  155.01892085 160.0195312  165.02014155 170.0207519
 175.02136225 180.0219726  185.02258295 190.0231933  195.02380365
 200.024414   205.02502435 210.0256347  215.02624505 220.0268554
 225.02746575 230.0280761  235.02868645 240.0292968  245.02990715
 250.0305175  255.03112785 260.0317382  265.03234855 270.0329589
 275.03356925 280.0341796  285.03478995 290.0354003  295.03601065
 300.036621   305.03723135 310.0378417  315.03845205 320.0390624
 325.03967275 330.0402831  335.04089345 340.0415038  345.04211415
 350.0427245  355.04333485 360.0439452  365.04455555 370.0451659
 375.04577625 380.0463866  385.04699695 390.0476073  395.04821765
 400.048828   405.04943835 410.0500487  415.05065905 420.0512694
 425.05187975 430.0524901  435.05310045 440.0537108  445.05432115
 450.0549315  455.05554185 460.0561522  465.05676255 470.0573729
 475.05798325 480.0585936  485.05920395 490.0598143  495.06042465
 500.061035   505.06164535 510.0622557  515.06286605 520.0634764
 525.06408675 530.0646971  535.06530745 540.0659178  545.06652815
 550.0671385  555.06774885 560.0683592  565.06896955 570.0695799
 575.07019025 580.0708006  585.07141095 590.0720213  595.07263165
 600.073242  ]
call without units
[100. 105. 110. 115. 120. 125. 130. 135. 140. 145. 150. 155. 160. 165.
 170. 175. 180. 185. 190. 195. 200. 205. 210. 215. 220. 225. 230. 235.
 240. 245. 250. 255. 260. 265. 270. 275. 280. 285. 290. 295. 300. 305.
 310. 315. 320. 325. 330. 335. 340. 345. 350. 355. 360. 365. 370. 375.
 380. 385. 390. 395. 400. 405. 410. 415. 420. 425. 430. 435. 440. 445.
 450. 455. 460. 465. 470. 475. 480. 485. 490. 495. 500. 505. 510. 515.
 520. 525. 530. 535. 540. 545. 550. 555. 560. 565. 570. 575. 580. 585.
 590. 595. 600.]
u.allclose(eval_result, call_result)=False

Versions

platform
--------
platform.platform() = 'Linux-6.6.36-x86_64-with-glibc2.39'
platform.version() = '#1-NixOS SMP PREEMPT_DYNAMIC Thu Jun 27 11:49:15 UTC 2024'
platform.python_version() = '3.12.4'

packages
--------
astropy              7.0.0.dev373+g15c40629c9.d20240703
numpy                2.0.0
scipy                1.13.1
matplotlib           3.9.0
pandas               2.2.2
pyerfa               2.0.1.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions