Skip to content

Commit

Permalink
use PythonCall instead of PyCall (#4)
Browse files Browse the repository at this point in the history
* use PythonCall instead of PyCall

* remove build.jl

* add concurrency blob

* drop stale comment

* README update

* Update README.md

---------

Co-authored-by: mich11 <michelle.fogerson@beacon.bio>
  • Loading branch information
palday and mich11 committed May 30, 2023
1 parent bcce447 commit 7bb2e90
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 210 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
name: CI
concurrency:
group: ${{ github.head_ref }}.ci
cancel-in-progress: true
on:
push:
paths-ignore:
Expand All @@ -18,7 +21,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1' # currently 1.6, but will hopefully change in the near future
- '1'
- '1.6'
- 'nightly'
os:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ docs/site/
# committed for packages, but should be committed for applications that require a static
# environment.
Manifest.toml

.CondaPkg/
10 changes: 10 additions & 0 deletions CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
channels = ["anaconda", "conda-forge"]
[deps]
# Conda package names and versions
python = ">=3.5,<4"

# CondaPkg uses mamba by default
fooof = ">=1.0, <2"

[pip.deps]
# Pip package names and versions
16 changes: 13 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
name = "PyFOOOF"
uuid = "bb993ee8-2a51-46a1-a315-55ff424041b4"
authors = ["Beacon Biosignals, Inc."]
version = "0.1.0"
version = "0.2.0"

[deps]
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"

[compat]
PyCall = "1.90"
Aqua = "0.5, 0.6"
PythonCall = "0.9"
Reexport = "1"
julia = "1.6"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "Test"]
307 changes: 131 additions & 176 deletions README.md

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions deps/build.jl

This file was deleted.

20 changes: 13 additions & 7 deletions src/PyFOOOF.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
module PyFOOOF

using PyCall

#####
##### init
##### Dependencies
#####

const fooof = PyNULL()
using Reexport

@reexport using PythonCall

######
###### Actual functionality
######

const fooof = PythonCall.pynew()

function __init__()
# all of this is __init__() so that it plays nice with precompilation
# see https://github.com/JuliaPy/PyCall.jl/#using-pycall-from-julia-modules
copy!(fooof, pyimport("fooof"))
# see https://github.com/cjdoris/PythonCall.jl/blob/5ea63f13c291ed97a8bacad06400acb053829dd4/src/Py.jl#L85-L96
PythonCall.pycopy!(fooof, pyimport("fooof"))
# don't eval into the module while precompiling; this breaks precompilation
# of downstream modules
# of downstream modules (see #4)
if ccall(:jl_generating_output, Cint, ()) == 0
# delegate everything else to fooof
for pn in propertynames(fooof)
Expand Down
3 changes: 0 additions & 3 deletions test/Project.toml

This file was deleted.

24 changes: 16 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using PyCall
using Aqua
using PyFOOOF
using Test

@testset "Aqua" begin
Aqua.test_all(PyFOOOF; ambiguities=false)
end

# test that this works
fooof = PyFOOOF.fooof

Expand All @@ -24,19 +28,23 @@ set_random_seed = pyimport("fooof.sim.utils").set_random_seed

f_range = [1, 50]
ap_params = [20, 2]
# XXX PyCall's aggressive conversion to NumPy arrays creates problems here
# so we force it be treated as raw Python code (py-prefix)
# and NOT be converted to a native Julia array (o-suffix)
gauss_params = py"[[10, 1.0, 2.5], [20, 0.8, 2], [32, 0.6, 1]]"
# FOOOF requires a list of lists for this, so we need to guarantee
# that Vector{<:Number} is not coverted to NumPy arrays
# NB: broadcasting so that we don't have a list of Vector{<:Number}, which
# would become a a list of NumPy arrays
gauss_params = pylist.([[10, 1.0, 2.5], [20, 0.8, 2], [32, 0.6, 1]])
nlv = 0.1
set_random_seed(21)

freqs, spectrum = gen_power_spectrum(f_range, ap_params, gauss_params, nlv)

fm = FOOOF(; peak_width_limits=py"[1, 8]"o, max_n_peaks=6, min_peak_height=0.4)
fm = FOOOF(; peak_width_limits=[1, 8], max_n_peaks=6, min_peak_height=0.4)
fm.fit(freqs, spectrum)

ground_truth = float.(py"$(gauss_params)")
actual_estimates = fm.gaussian_params_
# not worth adding a compat dependency just to get stack() for one test,
# ew this is messy without stack()
ground_truth = mapreduce(x -> pyconvert(Vector{Float64}, x)',
vcat, gauss_params; init=Matrix{Float64}(undef, 0, 3))
actual_estimates = pyconvert(Matrix, fm.gaussian_params_)
expected_estimates = [9.82 0.91 2.43; 20.03 0.82 1.94; 31.85 0.57 1.11]
@test actual_estimates expected_estimates atol=0.01

2 comments on commit 7bb2e90

@palday
Copy link
Member Author

@palday palday commented on 7bb2e90 May 30, 2023

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/84562

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 v0.2.0 -m "<description of version>" 7bb2e90b4a7b7236ac3c363e92449127e0a56dc3
git push origin v0.2.0

Please sign in to comment.