Skip to content

Commit

Permalink
update to pyo3 0.23 (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Dec 4, 2024
1 parent 0abb971 commit 32743e3
Show file tree
Hide file tree
Showing 30 changed files with 156 additions and 146 deletions.
64 changes: 34 additions & 30 deletions python/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ geoarrow = { path = "../rust/geoarrow" }
# geoarrow = { version = "0.4.0-beta.1" }
geozero = "0.14"
indexmap = "2.5.0"
numpy = "0.22"
numpy = "0.23"
object_store = "0.11"
parquet = "53"
pyo3 = { version = "0.22.0", features = ["hashbrown", "serde", "anyhow"] }
pyo3-arrow = "0.5.1"
pyo3 = { version = "0.23.0", features = ["hashbrown", "serde", "anyhow"] }
pyo3-arrow = "0.6"
serde_json = "1"
thiserror = "1"
10 changes: 8 additions & 2 deletions python/geoarrow-compute/src/algorithm/polylabel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ pub fn polylabel(py: Python, input: AnyNativeInput, tolerance: f64) -> PyGeoArro
match input {
AnyNativeInput::Array(arr) => {
let out = arr.as_ref().polylabel(tolerance)?;
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(out))).into_py(py))
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(out)))
.into_pyobject(py)?
.into_any()
.unbind())
}
AnyNativeInput::Chunked(chunked) => {
let out = chunked.as_ref().polylabel(tolerance)?;
Ok(PyChunkedNativeArray::new(Arc::new(out)).into_py(py))
Ok(PyChunkedNativeArray::new(Arc::new(out))
.into_pyobject(py)?
.into_any()
.unbind())
}
}
}
4 changes: 2 additions & 2 deletions python/geoarrow-compute/src/broadcasting/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pyo3::prelude::*;
// fn extract(_ob: &'a PyAny) -> PyResult<Self> {
// todo!()
// // Python::with_gil(|py| {
// // let pa = py.import_bound("pyarrow")?;
// // let pa = py.import("pyarrow")?;
// // let array = pa.getattr("Array")?;
// // if ob.hasattr("__arrow_c_array__")? {
// // let arr = from_py_array(ob)?;
Expand Down Expand Up @@ -41,7 +41,7 @@ impl<'a> FromPyObject<'a> for BroadcastableFloat {
fn extract_bound(_ob: &Bound<'a, PyAny>) -> PyResult<Self> {
todo!()
// Python::with_gil(|py| {
// let pa = py.import_bound("pyarrow")?;
// let pa = py.import("pyarrow")?;
// let array = pa.getattr("Array")?;
// if ob.hasattr("__arrow_c_array__")? {
// let arr = from_py_array(ob)?;
Expand Down
4 changes: 2 additions & 2 deletions python/geoarrow-compute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ fn ___version() -> &'static str {
fn check_debug_build(py: Python) -> PyResult<()> {
#[cfg(debug_assertions)]
{
let warnings_mod = py.import_bound(intern!(py, "warnings"))?;
let warnings_mod = py.import(intern!(py, "warnings"))?;
let warning = PyRuntimeWarning::new_err(
"geoarrow-rust-compute has not been compiled in release mode. Performance will be degraded.",
);
let args = PyTuple::new_bound(py, vec![warning.into_py(py)]);
let args = PyTuple::new(py, vec![warning])?;
warnings_mod.call_method1(intern!(py, "warn"), args)?;
}
Ok(())
Expand Down
10 changes: 4 additions & 6 deletions python/geoarrow-compute/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,20 @@ pub(crate) fn return_geometry_array(
) -> PyGeoArrowResult<PyObject> {
Ok(PyNativeArray::new(NativeArrayDyn::new(arr))
.to_geoarrow(py)?
.to_object(py))
.unbind())
}

pub(crate) fn return_chunked_geometry_array(
py: Python,
arr: Arc<dyn ChunkedNativeArray>,
) -> PyGeoArrowResult<PyObject> {
Ok(PyChunkedNativeArray::new(arr)
.to_geoarrow(py)?
.to_object(py))
Ok(PyChunkedNativeArray::new(arr).to_geoarrow(py)?.unbind())
}

pub(crate) fn return_array(py: Python, arr: PyArray) -> PyGeoArrowResult<PyObject> {
Ok(arr.to_arro3(py)?.to_object(py))
Ok(arr.to_arro3(py)?)
}

pub(crate) fn return_chunked_array(py: Python, arr: PyChunkedArray) -> PyGeoArrowResult<PyObject> {
Ok(arr.to_arro3(py)?.to_object(py))
Ok(arr.to_arro3(py)?)
}
8 changes: 4 additions & 4 deletions python/geoarrow-core/src/crs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ pub struct CRS(Value);
impl<'py> FromPyObject<'py> for CRS {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let py = ob.py();
let pyproj = py.import_bound(intern!(py, "pyproj"))?;
let pyproj = py.import(intern!(py, "pyproj"))?;
let crs_class = pyproj.getattr(intern!(py, "CRS"))?;

let mut ob = ob.clone();

// If the input is not a pyproj.CRS, call pyproj.CRS.from_user_input on it
if !ob.is_instance(&crs_class)? {
let args = PyTuple::new_bound(py, vec![ob]);
let args = PyTuple::new(py, vec![ob])?;
ob = crs_class.call_method1(intern!(py, "from_user_input"), args)?;
}

Expand All @@ -43,10 +43,10 @@ impl CRS {
}

pub fn to_pyproj(&self, py: Python) -> PyGeoArrowResult<PyObject> {
let pyproj = py.import_bound(intern!(py, "pyproj"))?;
let pyproj = py.import(intern!(py, "pyproj"))?;
let crs_class = pyproj.getattr(intern!(py, "CRS"))?;

let args = PyTuple::new_bound(py, vec![serde_json::to_string(&self.0)?]);
let args = PyTuple::new(py, vec![serde_json::to_string(&self.0)?])?;
let crs_obj = crs_class.call_method1(intern!(py, "from_json"), args)?;
Ok(crs_obj.into())
}
Expand Down
10 changes: 8 additions & 2 deletions python/geoarrow-core/src/ffi/to_python/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ pub fn native_array_to_pyobject(
py: Python,
arr: Arc<dyn NativeArray>,
) -> PyGeoArrowResult<PyObject> {
Ok(PyNativeArray::new(NativeArrayDyn::new(arr)).into_py(py))
Ok(PyNativeArray::new(NativeArrayDyn::new(arr))
.into_pyobject(py)?
.into_any()
.unbind())
}

pub fn chunked_native_array_to_pyobject(
py: Python,
arr: Arc<dyn ChunkedNativeArray>,
) -> PyGeoArrowResult<PyObject> {
Ok(PyChunkedNativeArray::new(arr).into_py(py))
Ok(PyChunkedNativeArray::new(arr)
.into_pyobject(py)?
.into_any()
.unbind())
}
4 changes: 2 additions & 2 deletions python/geoarrow-core/src/interop/geopandas/from_geopandas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ pub fn from_geopandas(py: Python, input: &Bound<PyAny>) -> PyGeoArrowResult<PyOb

// Note: I got an error in test_write_native_multi_points in `from_geopandas` with the WKB
// encoding
let kwargs = PyDict::new_bound(py);
let kwargs = PyDict::new(py);
kwargs.set_item("geometry_encoding", "geoarrow")?;
let table = input
.call_method(
intern!(py, "to_arrow"),
PyTuple::new_bound(py, std::iter::empty::<PyObject>()),
PyTuple::new(py, std::iter::empty::<PyObject>())?,
Some(&kwargs),
)?
.extract::<PyTable>()?;
Expand Down
6 changes: 2 additions & 4 deletions python/geoarrow-core/src/interop/geopandas/to_geopandas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use pyo3_geoarrow::PyGeoArrowResult;
pub fn to_geopandas(py: Python, input: PyObject) -> PyGeoArrowResult<PyObject> {
let geopandas_mod = import_geopandas(py)?;
let geodataframe_class = geopandas_mod.getattr(intern!(py, "GeoDataFrame"))?;
let gdf = geodataframe_class.call_method1(
intern!(py, "from_arrow"),
PyTuple::new_bound(py, vec![input]),
)?;
let gdf = geodataframe_class
.call_method1(intern!(py, "from_arrow"), PyTuple::new(py, vec![input])?)?;
Ok(gdf.into())
}
14 changes: 7 additions & 7 deletions python/geoarrow-core/src/interop/numpy/to_numpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ pub fn wkb_array_to_numpy(py: Python, arr: &geoarrow::array::WKBArray<i32>) -> P
));
}

let numpy_mod = py.import_bound(intern!(py, "numpy"))?;
let numpy_mod = py.import(intern!(py, "numpy"))?;

let args = (arr.len(),);
let kwargs = PyDict::new_bound(py);
let kwargs = PyDict::new(py);
kwargs.set_item("dtype", numpy_mod.getattr(intern!(py, "object_"))?)?;
let np_arr = numpy_mod.call_method(intern!(py, "empty"), args, Some(&kwargs))?;

for (i, wkb) in arr.iter_values().enumerate() {
np_arr.set_item(i, PyBytes::new_bound(py, wkb.as_ref()))?;
np_arr.set_item(i, PyBytes::new(py, wkb.as_ref()))?;
}

Ok(np_arr.to_object(py))
Ok(np_arr.into_pyobject(py)?.into_any().unbind())
}

pub fn chunked_wkb_array_to_numpy(
py: Python,
arr: geoarrow::chunked_array::ChunkedWKBArray<i32>,
) -> PyResult<PyObject> {
let numpy_mod = py.import_bound(intern!(py, "numpy"))?;
let numpy_mod = py.import(intern!(py, "numpy"))?;
let shapely_chunks = arr
.chunks()
.iter()
.map(|chunk| Ok(wkb_array_to_numpy(py, chunk)?.to_object(py)))
.map(|chunk| wkb_array_to_numpy(py, chunk))
.collect::<PyResult<Vec<_>>>()?;
let np_arr = numpy_mod.call_method1(intern!(py, "concatenate"), (shapely_chunks,))?;
Ok(np_arr.to_object(py))
Ok(np_arr.into_pyobject(py)?.into_any().unbind())
}
Loading

0 comments on commit 32743e3

Please sign in to comment.