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

Migrate to use PythonCall.jl #2

Merged
merged 7 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 1 addition & 23 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.8'
- '1.10'
- 'nightly'
os:
- ubuntu-latest
Expand All @@ -33,28 +33,6 @@ jobs:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v1
- name: Fix weird Conda.jl/PyCall.jl build error
env:
PYTHON: ""
shell: julia --color=yes {0}
run: |
using Pkg
try
Pkg.instantiate()
println("Successfully instantiated the test environment")
catch e
display(e)
end
ENV["PYTHON"] = ""
Pkg.add("Conda")
println("Try building Conda and PyCall")
try
Pkg.build("Conda")
using Conda
println("Successfully built Conda")
catch e
display(e)
end
- uses: julia-actions/julia-buildpkg@v1
env:
PYTHON: "" # for conda packages
Expand Down
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
5 changes: 5 additions & 0 deletions CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
channels = ["conda-forge"]

[deps]
andes = ""
kvxopt = ">1.3.0.0"
13 changes: 6 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
name = "Andes"
uuid = "93a26e3f-343a-4ab9-b467-a68c67574964"
authors = ["Hantao Cui <hcui7@utk.edu>"]
version = "0.2.0"
authors = ["Hantao Cui <hcui@ieee.org>"]
version = "1.0.0"

[deps]
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
PyCall = "1.93"
Conda = "1.7"
julia = "1"
PythonCall = "0.9.15"
julia = "1.3.1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
6 changes: 3 additions & 3 deletions deps/build.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Pkg
using PyCall
using Conda
using PythonCall
using CondaPkg

try
pyimport("andes")
pyimport("kvxopt")
catch
@warn "PyCall is not configured to an existing Python env."
@warn "Andes.jl will use Conda for PyCall and install andes."
Expand Down
23 changes: 10 additions & 13 deletions src/Andes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@ __precompile__()

module Andes

using Conda
using PyCall
using PythonCall

import SparseArrays
const py = PythonCall.pynew()

const py = PyNULL()

include("kvxopt.jl")
include("kvxopt_pythoncall.jl")

function __init__()
copy!(py, pyimport_conda("andes", "andes", "conda-forge"))
PythonCall.pycopy!(py, pyimport("andes"))

pytype_mapping(pyimport("kvxopt").spmatrix, SparseArrays.SparseMatrixCSC)
PythonCall.pyconvert_add_rule("kvxopt.base:spmatrix",
AbstractSparseMatrixCSC,
spmatrix_to_julia,
)
end

export pyconvert, pytype

# --- export ---
export convert;

end # module

end
45 changes: 0 additions & 45 deletions src/kvxopt.jl

This file was deleted.

22 changes: 22 additions & 0 deletions src/kvxopt_pythoncall.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CondaPkg
using SparseArrays: AbstractSparseMatrixCSC, SparseMatrixCSC
using PythonCall: pyconvert_add_rule

"""
Convert KVXOPT.spmatrix to SparseMatrixCSC
"""
function spmatrix_to_julia(S::Type{T}, x::Py) where {T<:AbstractSparseMatrixCSC}
# Ensure x is a KVXOPT spmatrix
if Bool(pytype(x) != pyimport("kvxopt").spmatrix)
throw(ArgumentError("x must be a KVXOPT spmatrix"))
end

# Extract the size, I, J, and V arrays from the spmatrix and explicitly convert them
m, n = pyconvert(Tuple{Int64,Int64}, x.size)
I = pyconvert(Vector{Int64}, x.CCS[0]) .+ 1
J = pyconvert(Vector{Int64}, x.CCS[1]) .+ 1
V = pyconvert(Vector{Float64}, x.CCS[2])

# Create and return the SparseMatrixCSC
return PythonCall.pyconvert_return(SparseMatrixCSC(m, n, I, J, V))
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
using Test

include("test_andes_py.jl")
include("test_andes_pythoncall.jl")
12 changes: 0 additions & 12 deletions test/test_andes_py.jl

This file was deleted.

21 changes: 21 additions & 0 deletions test/test_andes_pythoncall.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Test
using Andes
using PythonCall
using SparseArrays

@testset "Test Andes functionalities" begin
@testset "SparseMatrixCSC conversion from Andes system example" begin
ss = Andes.py.system.example()
converted_matrix = pyconvert(SparseMatrixCSC, ss.dae.gy)
@test converted_matrix isa SparseMatrixCSC
@test size(converted_matrix) == (34, 34)
end

@testset "Power flow run" begin
Andes.py.config_logger(40)
kundur = Andes.py.utils.paths.get_case("kundur/kundur_full.xlsx")
system = Andes.py.run(kundur, no_output=true)

@test Bool(system.PFlow.converged == true)
end
end
Loading