Skip to content

Commit

Permalink
Merge pull request #2 from cuihantao/migrate-PythonCall
Browse files Browse the repository at this point in the history
Migrate to use PythonCall.jl
  • Loading branch information
cuihantao authored Feb 4, 2024
2 parents e1bcefc + 2d68d8d commit c6f942d
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 104 deletions.
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

4 comments on commit c6f942d

@cuihantao
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/100251

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.0 -m "<description of version>" c6f942d442c82191a3d87c9b55ecaa9c84cf153f
git push origin v1.0.0

@cuihantao
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register

Release notes:

Breaking changes

  • Migrated from PyCall.jl to PythonCall.jl. Dropped dependency on PyCall.jl. User code needs to be updated accordingly.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request updated: JuliaRegistries/General/100251

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.0 -m "<description of version>" c6f942d442c82191a3d87c9b55ecaa9c84cf153f
git push origin v1.0.0

Please sign in to comment.