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

switching to PythonCall.jl #32

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Manifest.toml
/docs/build/

dev
test/output
test/output
.CondaPkg
4 changes: 4 additions & 0 deletions CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[deps]
python = "3.9"
Copy link
Member Author

Choose a reason for hiding this comment

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

Attempting to install numpy on windows with python 3.10 seems to be running into this, so staying on 3.9 for now

batman-package = ""
numpy = ""
3 changes: 2 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ Bijectors = "76274a88-744f-5084-9051-94815aaf08c4"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
HypothesisTests = "09f84164-cd44-5f33-b23f-e6b0d136a0d5"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
82 changes: 39 additions & 43 deletions test/orbits/keplerian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ using Transits.Orbits: KeplerianOrbit, flip,
stringify_units

# Setup python env
import Pkg
ENV["PYTHON"] = ""
Pkg.build("PyCall")
using PyCall, Conda
Conda.add(["batman-package"]; channel="conda-forge")
using PythonCall, CondaPkg
CondaPkg.add("batman-package")

# Constants
const G_nom = 2942.2062175044193 # Rsun^3/Msun/d^2
Expand All @@ -32,49 +29,46 @@ as_matrix(pos) = reinterpret(reshape, Float64, pos) |> permutedims

@testset "KeplerianOrbit: sky coords" begin
# Comparison coords from `batman`
py"""
@pyexec """
import numpy as np
from batman import _rsky

def sky_coords():
t = np.linspace(-100, 100, 1_000)

t0, period, a, e, omega, incl = (
x.flatten()
for x in np.meshgrid(
np.linspace(-5.0, 5.0, 2),
np.exp(np.linspace(np.log(5.0), np.log(50.0), 3)),
np.linspace(50.0, 100.0, 2),
np.linspace(0.0, 0.9, 5),
np.linspace(-np.pi, np.pi, 3),
np.arccos(np.linspace(0, 1, 5)[:-1]),
)
t = np.linspace(-100, 100, 1_000)

t0, period, a, e, omega, incl = (
x.flatten()
for x in np.meshgrid(
np.linspace(-5.0, 5.0, 2),
np.exp(np.linspace(np.log(5.0), np.log(50.0), 3)),
np.linspace(50.0, 100.0, 2),
np.linspace(0.0, 0.9, 5),
np.linspace(-np.pi, np.pi, 3),
np.arccos(np.linspace(0, 1, 5)[:-1]),
)
)

r_batman = np.empty((len(t), len(t0)))

for i in range(len(t0)):
r_batman[:, i] = _rsky._rsky(
t, t0[i], period[i], a[i], incl[i], e[i], omega[i], 1, 1
)

m = r_batman < 100.0
r_batman = np.empty((len(t), len(t0)))

return {
"m_sum" : m.sum().item(), # Save native Int format
"r_batman" : r_batman,
"m" : m,
"t" : t,
"t0" : t0,
"period" : period,
"a" : a,
"e" : e,
"omega" : omega,
"incl" : incl,
}
for i in range(len(t0)):
r_batman[:, i] = _rsky._rsky(
t, t0[i], period[i], a[i], incl[i], e[i], omega[i], 1, 1
)

"""
sky_coords = py"sky_coords"()
m = r_batman < 100.0

sky_coords = {
"m_sum" : m.sum().item(), # Save native Int format
"r_batman" : r_batman,
"m" : m,
"t" : t,
"t0" : t0,
"period" : period,
"a" : a,
"e" : e,
"omega" : omega,
"incl" : incl,
}
""" => sky_coords::Dict

# Create comparison orbits from Transits.jl
orbits = [
Expand Down Expand Up @@ -288,7 +282,8 @@ end
)

# Comparison coords from `batman`
py"""
@pyexec """
global np, _rsky, small_star
import numpy as np
from batman import _rsky

Expand All @@ -314,7 +309,8 @@ end
"m": m,
}
"""
small_star = py"small_star"(orbit.period, orbit.t0, orbit.aR_star, orbit.incl, orbit.ecc, orbit.omega)
small_star_py = @pyeval("small_star")(orbit.period, orbit.t0, orbit.aR_star, orbit.incl, orbit.ecc, orbit.omega)
small_star = pyconvert(Dict{String, Vector}, small_star_py)

# Compare
t = small_star["t"]
Expand Down