Skip to content

API suggestion: from diffeqpy import de - #24

Merged
ChrisRackauckas merged 5 commits into
SciML:masterfrom
tkf:module
Apr 28, 2018
Merged

ChrisRackauckas merged 5 commits into
SciML:masterfrom
tkf:module

Conversation

@tkf

@tkf tkf commented Apr 27, 2018

Copy link
Copy Markdown
Contributor

This patch introduces tiny changes in API for a bit of Python-user friendliness:

  1. from diffeqpy import de instead of import diffeqpy; de = diffeqpy.setup()
  2. de.solve instead of de.pysolve

I suggest to not pull this patch at the moment since import diffeqpy.de prints out a lot of UndefVarError(...) messages presumably due to export <undefined variable name> which, I think, would scare users.

I'm actually not sure if this change worth a trouble for fixing the UndefVarError(...) messages. That would require going over many files in DifferentialEquations.jl projects. But probably fixing the UndefVarError problem makes sense anyway since it's kind of a bug in DifferentialEquations.jl anyway (though I suppose it's not harmful)?

@tkf

tkf commented Apr 27, 2018

Copy link
Copy Markdown
Contributor Author

I just realize pytest's IO capture kicks in so you can't see UndefVarError in Travis. Easiest way to produce it is to checkout this PR and do import diffeqpy.de. You can also run tox -e py36 -- -sv.

For example, it prints UndefVarError(:CompoundConstantRateJump) and if I search CompoundConstantRateJump in DifferentialEquations.jl, I see that it's not defined anywhere: https://github.com/search?q=org%3AJuliaDiffEq+CompoundConstantRateJump&type=Code Is it old struct or something? Anyway, it seems there are many such examples.

@ChrisRackauckas

Copy link
Copy Markdown
Member

If it was a warning then I would just Surpressor.jl it. As an error, that seems fragile.

@tkf

tkf commented Apr 27, 2018

Copy link
Copy Markdown
Contributor Author

Well it's an error in Julia side:

julia> using DifferentialEquations

julia> :CompoundConstantRateJump in names(DifferentialEquations)
true

julia> DifferentialEquations.CompoundConstantRateJump
ERROR: UndefVarError: CompoundConstantRateJump not defined

pyjulia just tries to suppresses it (but somehow it leaks out):

def isafunction(julia, julia_name, mod_name=""):
    code = "isa({}, Function)".format(julia_name)
    if mod_name:
        code = "isa({}.{}, Function)".format(mod_name, julia_name)
    try:
        return julia.eval(code)
    except:
        return False

https://github.com/JuliaPy/pyjulia/blob/2eb704c33d6f639b41300023e16f910b5945600e/julia/core.py#L155-L162

@ChrisRackauckas

Copy link
Copy Markdown
Member

I always found it odd that Julia doesn't error if you don't remember to get rid of an export statement. That fact will now bite us if we rely on making sure all exports are correct, since this would be the only library that would show the error in its tests...

@tkf

tkf commented Apr 28, 2018

Copy link
Copy Markdown
Contributor Author

I agree that exporting undefined variable being not an error in Julia is strange. Personally, I hope it become so in the future.

But I think, ideally, it is better to try not export undefined variable even in current Julia situation. For example, newbie user may be puzzled when tab completion yields undefined variables:

julia> using DifferentialEquations

julia> Compo<TAB>
CompositeAlgorithm       CompoundConstantRateJump
CompositeException
julia> CompoundConstantRateJump
ERROR: UndefVarError: CompoundConstantRateJump not defined

Well, I know it's just a nitpicking :) Anyway, I think it is solvable in pyjulia side: JuliaPy/pyjulia#159

@ChrisRackauckas

Copy link
Copy Markdown
Member

Is there a stopgap solution somewhere here? I want to merge the syntax changes but don't want it to be broken

@tkf

tkf commented Apr 28, 2018

Copy link
Copy Markdown
Contributor Author

Implemented a quick-and-dirty solution: ad28915

Comment thread diffeqpy/de.py
# Suppress many UndefVarError printed out.
# See: https://github.com/JuliaPy/pyjulia/issues/159
# https://github.com/JuliaDiffEq/diffeqpy/pull/24
try:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

so what exactly is this doing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The problematic part in pyjulia is

self.api.jl_call2(self.api.show, stderr, exoc)

in Julia.check_exception (https://github.com/JuliaPy/pyjulia/blob/2eb704c33d6f639b41300023e16f910b5945600e/julia/core.py#L416-L432) which is called unconditionally before raise JuliaError. The JuliaError is suppressed by try-except in pyjulia module loader but call to Base.show above is not.

What I'm doing here is to monkey-patch self.api so that self.api.show points to Julia's identity function instead of show.

Wait, identity does not take two arguments. Why does it work?! I should probably use other function. Fixing it...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess fix this up to something that's supposed to work (?) and we're good?

@@ -1,9 +1,7 @@
import diffeqpy
from .. import de

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what's this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's Python's relative import syntax. from .. import de is equivalent to from diffeqpy import de here. I just use it whenever I can, since it's easier to rename the package, for example.

@ChrisRackauckas ChrisRackauckas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

tkf added 3 commits April 27, 2018 20:10
Using identity "worked" somehow in the sense it suppresses UndefVarError
messages.  But identity cannot be called with two arguments.  It is
probably safer to use tuple function here.
@tkf

tkf commented Apr 28, 2018

Copy link
Copy Markdown
Contributor Author

So I fixed the potential issue I mentioned in https://github.com/JuliaDiffEq/diffeqpy/pull/24/files/ad289154df70a632c9f853f7123f1440c742122a#r184837524, put some comments, and tweak the code a little bit. Should be good to go.

@ChrisRackauckas
ChrisRackauckas merged commit a845eb2 into SciML:master Apr 28, 2018
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.

2 participants