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

fix observed for DEintegrator #595

Closed
wants to merge 5 commits into from

Conversation

oscardssmith
Copy link
Contributor

This still needs a test, but it turns out both of these methods are unnecessary (since they just fall back to working on the ScimlFunction) and the observed implementation here is just incorrect.

Copy link

codecov bot commented Jan 10, 2024

Codecov Report

Attention: 10 lines in your changes are missing coverage. Please review.

Comparison is base (0c62711) 26.25% compared to head (1cb0ec2) 30.53%.
Report is 4 commits behind head on master.

Files Patch % Lines
src/scimlfunctions.jl 0.00% 9 Missing ⚠️
src/problems/problem_interface.jl 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #595      +/-   ##
==========================================
+ Coverage   26.25%   30.53%   +4.27%     
==========================================
  Files          54       54              
  Lines        4113     4120       +7     
==========================================
+ Hits         1080     1258     +178     
+ Misses       3033     2862     -171     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@oscardssmith
Copy link
Contributor Author

tests added. I think this is ready to merge.

@AayushSabharwal
Copy link
Member

AayushSabharwal commented Jan 11, 2024

I get the is_observed fallback, but what is wrong with the observed implementation?

Without it observed expressions won't work for integrators. i.e. getu(integrator, sys.x + sys.y)(integrator) would error?

@AayushSabharwal
Copy link
Member

I guess the is_observed method can also be removed for AbstractSciMLProblem in problems/problem_interface.jl as well.

@oscardssmith
Copy link
Contributor Author

the correct observed function would just call getobserved. This version both gets the observed function and then calls it with work incorrect arguments (it doesn't pass in u, p, or t)

@oscardssmith
Copy link
Contributor Author

I guess the is_observed method can also be removed for AbstractSciMLProblem in problems/problem_interface.jl as well.

good call on this. When removing it, I discovered that the observed function was broken in the same way as the integrator one 😆

@AayushSabharwal
Copy link
Member

the correct observed function would just call getobserved. This version both gets the observed function and then calls it with work incorrect arguments (it doesn't pass in u, p, or t)

Where is this function defined? There's no method that SII.observed will fall back to now.

Also you'll need to disambiguate between ModelingToolkit.observed and SII.observed in the test since they're both loaded

@oscardssmith
Copy link
Contributor Author

SII.observed falls back to the definition in SymolicIndexingInterfase (specifically observed(sys, sym) = observed(symbolic_container(sys), sym).

@oscardssmith
Copy link
Contributor Author

lets see how CI likes this.

@AayushSabharwal
Copy link
Member

SII.observed falls back to the definition in SymolicIndexingInterfase (specifically observed(sys, sym) = observed(symbolic_container(sys), sym).

Yeah but eventually something needs to implement it, because otherwise it'll fallback to the system which doesn't

@oscardssmith
Copy link
Contributor Author

it gets implimented by SciMLFunction.

Copy link
Member

@AayushSabharwal AayushSabharwal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only just realized that SciMLFunction already implements this. I was waiting for the methods to be added 😅 Sorry for the delay. Could you make sure the symbolic indexing tests pass before merging?

@oscardssmith
Copy link
Contributor Author

I believe the symbolic indexing tests are passing (they run locally at least), but I'm having some trouble figuring out which of the CI runs is the related one.

@AayushSabharwal
Copy link
Member

It's CI / test (Downstream, 1) which seems to fail before it even gets to that point. The error seems to be an Optimization.jl thing?

@oscardssmith
Copy link
Contributor Author

Oh, I think the Optimization problem is #600

@oscardssmith
Copy link
Contributor Author

rebased. Let's try this again.

@ChrisRackauckas
Copy link
Member

@Vaibhavdixit02 I thought the Optimization stats merging was handled?

@Vaibhavdixit02
Copy link
Member

It is, this looks like something else iiuc. The downstream CI right?

@ChrisRackauckas
Copy link
Member

oh, I must've not refreshed. It's an @AayushSabharwal issue then 😅

@AayushSabharwal
Copy link
Member

AayushSabharwal commented Jan 22, 2024

Yeah this is a bad test I fixed in #584

get_obs = getu(sys_simplified, lorenz1.x + lorenz2.x)
get_obs_arr = getu(sys_simplified, [lorenz1.x + lorenz2.x, lorenz1.y + lorenz2.y])

should be

get_obs = getu(prob, lorenz1.x + lorenz2.x)
get_obs_arr = getu(prob, [lorenz1.x + lorenz2.x, lorenz1.y + lorenz2.y])

@ChrisRackauckas
Copy link
Member

which PR?

@AayushSabharwal
Copy link
Member

#584

@AayushSabharwal
Copy link
Member

Bump. This is kinda needed for MTK

@ChrisRackauckas
Copy link
Member

@oscardssmith
Copy link
Contributor Author

agreed. I'm unsure why this pr would have regressed inference though.

@AayushSabharwal
Copy link
Member

Could you try rebasing this?

@oscardssmith
Copy link
Contributor Author

rebased.

@ChrisRackauckas
Copy link
Member

Downstream still failing.

@oscardssmith
Copy link
Contributor Author

yes. I still have no idea why this PR causes us to lose inference precision.

@AayushSabharwal
Copy link
Member

AayushSabharwal commented Feb 5, 2024

Changing the is_observed function in scimlfunctions.jl to

SymbolicIndexingInterface.is_observed(fn::AbstractSciMLFunction, sym) = has_sys(fn) ? is_observed(fn.sys, sym) : has_observed(fn)

helps, but the same error occurs later on.

@AayushSabharwal
Copy link
Member

AayushSabharwal commented Feb 5, 2024

Changing SII.observed in the same file to:

function SymbolicIndexingInterface.observed(fn::AbstractSciMLFunction, sym)
  if has_observed(fn)
    if is_time_dependent(fn)
      return if hasmethod(fn.observed, Tuple{typeof(sym)})
        fn.observed(sym)
      else
        let obs = fn.observed, sym = sym
          (u, p, t) -> obs(sym, u, p, t)
        end
      end
    else
      return if hasmethod(fn.observed, Tuple{typeof(sym)})
        fn.observed(sym)
      else
        let obs = fn.observed, sym = sym
          (u, p) -> obs(sym, u, p)
        end
      end
    end
  end
  error("SciMLFunction does not have observed")
end

Fixes this. I'm not completely sure why. We need the if condition because the generated observed for SDEs doesn't allow calling it with only a symbol (sdeprob.f.observed(sym) throws a MethodError). hasmethod is static so the branches should be removed at compile time.

@oscardssmith
Copy link
Contributor Author

Something feels deeply wrong to me here. Should sdeprob.f.observed(sym) just work? where is the ODE version done ( odeprob.f.observed(sym))?

@oscardssmith
Copy link
Contributor Author

that seems to have made things much worse.

@AayushSabharwal
Copy link
Member

Should sdeprob.f.observed(sym) just work? where is the ODE version done ( odeprob.f.observed(sym))?

Yeah it should work. All of this is implemented in MTK, and the SDE version just isn't up to spec.

that seems to have made things much worse.

Yeah, that's weird.

@oscardssmith
Copy link
Contributor Author

since our timezones match quite poorly, would you mind taking this over (e.g. possibly making your own branch so you can push without waiting for me?) it would be very nice to get this merged soon.

@AayushSabharwal
Copy link
Member

AayushSabharwal commented Feb 6, 2024

Succeeded by #615

I can't close this PR

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

Successfully merging this pull request may close these issues.

4 participants