Skip to content

Commit 3b0409e

Browse files
committed
use new pyo3 0.22 bound syntax
1 parent ef2b43c commit 3b0409e

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

src/python/analytic.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl AnalyticBeam {
2929
/// bowties per tile, 8 for CRAM for a total of 64 per tile). The defaults
3030
/// is mwa_pb behaviour with 4 bowties per row.
3131
#[new]
32-
#[pyo3(text_signature = "(rts_behaviour, dipole_height, bowties_per_row)")]
32+
#[pyo3(signature = (rts_behaviour=None, dipole_height=None, bowties_per_row=None))]
3333
fn new(
3434
rts_behaviour: Option<bool>,
3535
dipole_height: Option<f64>,
@@ -55,9 +55,7 @@ impl AnalyticBeam {
5555
/// this number or double; if the former is given, then these map 1:1
5656
/// with bowties. If double are given, then the *smallest* of the two amps
5757
/// corresponding to a bowtie's dipoles is used.
58-
#[pyo3(
59-
text_signature = "(az_rad, za_rad, freq_hz, delays, amps, latitude_rad, norm_to_zenith)"
60-
)]
58+
#[pyo3(signature = (az_rad, za_rad, freq_hz, delays, amps, latitude_rad, norm_to_zenith=None))]
6159
#[allow(clippy::too_many_arguments)]
6260
fn calc_jones<'py>(
6361
&self,
@@ -69,7 +67,7 @@ impl AnalyticBeam {
6967
amps: Vec<f64>,
7068
latitude_rad: f64,
7169
norm_to_zenith: Option<bool>,
72-
) -> PyResult<&'py PyArray1<c64>> {
70+
) -> PyResult<Bound<'py, PyArray1<c64>>> {
7371
let jones = self.beam.calc_jones_pair(
7472
az_rad,
7573
za_rad,
@@ -83,7 +81,7 @@ impl AnalyticBeam {
8381
norm_to_zenith.unwrap_or(false),
8482
)?;
8583
let jones_py: Vec<c64> = jones.iter().map(|c| c64::new(c.re, c.im)).collect();
86-
let np_array = PyArray1::from_vec(py, jones_py);
84+
let np_array = PyArray1::from_vec_bound(py, jones_py);
8785
Ok(np_array)
8886
}
8987

@@ -95,9 +93,7 @@ impl AnalyticBeam {
9593
/// or double; if the former is given, then these map 1:1 with bowties. If
9694
/// double are given, then the *smallest* of the two amps corresponding to a
9795
/// bowtie's dipoles is used.
98-
#[pyo3(
99-
text_signature = "(az_rad, za_rad, freq_hz, delays, amps, latitude_rad, norm_to_zenith)"
100-
)]
96+
#[pyo3(signature = (az_rad, za_rad, freq_hz, delays, amps, latitude_rad, norm_to_zenith=None))]
10197
#[allow(clippy::too_many_arguments)]
10298
fn calc_jones_array<'py>(
10399
&self,
@@ -109,7 +105,7 @@ impl AnalyticBeam {
109105
amps: Vec<f64>,
110106
latitude_rad: f64,
111107
norm_to_zenith: Option<bool>,
112-
) -> PyResult<&'py PyArray2<c64>> {
108+
) -> PyResult<Bound<'py, PyArray2<c64>>> {
113109
let jones = self.beam.calc_jones_array_pair(
114110
&az_rad,
115111
&za_rad,
@@ -132,7 +128,7 @@ impl AnalyticBeam {
132128
// SAFETY: new_cap == old_cap * N, align_of::<C64>() == align_of::<Jones>()
133129
let flat = unsafe { Vec::from_raw_parts(new_ptr, new_len, new_cap) };
134130
let a2 = Array2::from_shape_vec((old_len, 4), flat).unwrap();
135-
Ok(a2.into_pyarray(py))
131+
Ok(a2.into_pyarray_bound(py))
136132
}
137133

138134
/// Calculate the Jones matrices for multiple directions given a pointing
@@ -144,9 +140,7 @@ impl AnalyticBeam {
144140
/// `delays_array` and `amps_array` have the same restrictions as `delays`
145141
/// and `amps` in `calc_jones`.
146142
#[cfg(any(feature = "cuda", feature = "hip"))]
147-
#[pyo3(
148-
text_signature = "(az_rad, za_rad, freqs_hz, delays_array, amps_array, latitude_rad, norm_to_zenith)"
149-
)]
143+
#[pyo3(signature = (az_rad, za_rad, freqs_hz, delays_array, amps_array, latitude_rad, norm_to_zenith=None))]
150144
#[allow(clippy::too_many_arguments)]
151145
fn calc_jones_gpu<'py>(
152146
&self,
@@ -158,7 +152,7 @@ impl AnalyticBeam {
158152
amps_array: Vec<f64>,
159153
latitude_rad: f64,
160154
norm_to_zenith: Option<bool>,
161-
) -> PyResult<&'py PyArray4<GpuComplex>> {
155+
) -> PyResult<Bound<'py, PyArray4<GpuComplex>>> {
162156
// hyperbeam expects ints for the frequencies. Convert them to make sure
163157
// everything's OK.
164158
let freqs: Vec<u32> = freqs_hz.iter().map(|&f| f.round() as _).collect();
@@ -188,14 +182,14 @@ impl AnalyticBeam {
188182
// Use unsafe code to ensure that no useless copying is done!
189183
// https://users.rust-lang.org/t/sound-conversion-from-vec-num-complex-complex64-4-to-ndarray-array2-num-complex-complex64-without-copying/78973/2
190184
let old_dim = jones.dim();
191-
let mut jones = std::mem::ManuallyDrop::new(jones.into_raw_vec());
185+
let mut jones = std::mem::ManuallyDrop::new(jones.into_raw_vec_and_offset().0);
192186

193187
let new_len = jones.len() * 4;
194188
let new_cap = jones.capacity() * 4;
195189
let new_ptr = jones.as_mut_ptr() as *mut GpuComplex;
196190
// SAFETY: new_cap == old_cap * N, align_of::<Complex>() == align_of::<Jones>()
197191
let flat = unsafe { Vec::from_raw_parts(new_ptr, new_len, new_cap) };
198192
let a4 = Array4::from_shape_vec((old_dim.0, old_dim.1, old_dim.2, 4), flat).unwrap();
199-
Ok(a4.into_pyarray(py))
193+
Ok(a4.into_pyarray_bound(py))
200194
}
201195
}

src/python/fee.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl FEEBeam {
2828
/// calculations. If the path to the beam HDF5 file is not given, then the
2929
/// `MWA_BEAM_FILE` environment variable is used.
3030
#[new]
31-
#[pyo3(text_signature = "(hdf5_file)")]
31+
#[pyo3(signature = (hdf5_file))]
3232
fn new(hdf5_file: Option<PyObject>) -> PyResult<Self> {
3333
let strct = match hdf5_file {
3434
Some(f) => {
@@ -64,7 +64,7 @@ impl FEEBeam {
6464
/// elements; if 16 are given, then these map 1:1 with dipoles, otherwise
6565
/// the first 16 are for X dipole elements, and the next 16 are for Y.
6666
#[pyo3(
67-
text_signature = "(az_rad, za_rad, freq_hz, delays, amps, norm_to_zenith, latitude_rad, iau_order)"
67+
signature = (az_rad, za_rad, freq_hz, delays, amps, norm_to_zenith, latitude_rad=None, iau_order=None)
6868
)]
6969
#[allow(clippy::too_many_arguments)]
7070
fn calc_jones<'py>(
@@ -78,7 +78,7 @@ impl FEEBeam {
7878
norm_to_zenith: bool,
7979
latitude_rad: Option<f64>,
8080
iau_order: Option<bool>,
81-
) -> PyResult<&'py PyArray1<c64>> {
81+
) -> PyResult<Bound<'py, PyArray1<c64>>> {
8282
let jones = self.beam.calc_jones_pair(
8383
az_rad,
8484
za_rad,
@@ -93,7 +93,7 @@ impl FEEBeam {
9393
iau_order.unwrap_or(false),
9494
)?;
9595
let jones_py: Vec<c64> = jones.iter().map(|c| c64::new(c.re, c.im)).collect();
96-
let np_array = PyArray1::from_vec(py, jones_py);
96+
let np_array = PyArray1::from_vec_bound(py, jones_py);
9797
Ok(np_array)
9898
}
9999

@@ -109,7 +109,7 @@ impl FEEBeam {
109109
/// elements; if 16 are given, then these map 1:1 with dipoles, otherwise
110110
/// the first 16 are for X dipole elements, and the next 16 are for Y.
111111
#[pyo3(
112-
text_signature = "(az_rad, za_rad, freq_hz, delays, amps, norm_to_zenith, latitude_rad, iau_order)"
112+
signature = (az_rad, za_rad, freq_hz, delays, amps, norm_to_zenith, latitude_rad=None, iau_order=None)
113113
)]
114114
#[allow(clippy::too_many_arguments)]
115115
fn calc_jones_array<'py>(
@@ -123,7 +123,7 @@ impl FEEBeam {
123123
norm_to_zenith: bool,
124124
latitude_rad: Option<f64>,
125125
iau_order: Option<bool>,
126-
) -> PyResult<&'py PyArray2<c64>> {
126+
) -> PyResult<Bound<'py, PyArray2<c64>>> {
127127
let jones = self.beam.calc_jones_array_pair(
128128
&az_rad,
129129
&za_rad,
@@ -147,12 +147,12 @@ impl FEEBeam {
147147
// SAFETY: new_cap == old_cap * N, align_of::<C64>() == align_of::<Jones>()
148148
let flat = unsafe { Vec::from_raw_parts(new_ptr, new_len, new_cap) };
149149
let a2 = Array2::from_shape_vec((old_len, 4), flat).unwrap();
150-
Ok(a2.into_pyarray(py))
150+
Ok(a2.into_pyarray_bound(py))
151151
}
152152

153153
/// Get the available frequencies inside the HDF5 file.
154-
fn get_fee_beam_freqs<'py>(&self, py: Python<'py>) -> &'py PyArray1<u32> {
155-
self.beam.get_freqs().to_vec().into_pyarray(py)
154+
fn get_fee_beam_freqs<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<u32>> {
155+
self.beam.get_freqs().to_vec().into_pyarray_bound(py)
156156
}
157157

158158
/// Given a frequency in Hz, get the closest available frequency inside the
@@ -172,7 +172,7 @@ impl FEEBeam {
172172
/// for an explanation).
173173
#[cfg(any(feature = "cuda", feature = "hip"))]
174174
#[pyo3(
175-
text_signature = "(az_rad, za_rad, freqs_hz, delays_array, amps_array, norm_to_zenith, latitude_rad, iau_order)"
175+
signature = (az_rad, za_rad, freqs_hz, delays_array, amps_array, norm_to_zenith, latitude_rad=None, iau_order=None)
176176
)]
177177
#[allow(clippy::too_many_arguments)]
178178
fn calc_jones_gpu<'py>(
@@ -186,7 +186,7 @@ impl FEEBeam {
186186
norm_to_zenith: bool,
187187
latitude_rad: Option<f64>,
188188
iau_order: Option<bool>,
189-
) -> PyResult<&'py PyArray4<GpuComplex>> {
189+
) -> PyResult<Bound<'py, PyArray4<GpuComplex>>> {
190190
// hyperbeam expects ints for the frequencies. Convert them to make sure
191191
// everything's OK.
192192
let freqs: Vec<u32> = freqs_hz.iter().map(|&f| f.round() as _).collect();
@@ -212,14 +212,14 @@ impl FEEBeam {
212212
// Use unsafe code to ensure that no useless copying is done!
213213
// https://users.rust-lang.org/t/sound-conversion-from-vec-num-complex-complex64-4-to-ndarray-array2-num-complex-complex64-without-copying/78973/2
214214
let old_dim = jones.dim();
215-
let mut jones = std::mem::ManuallyDrop::new(jones.into_raw_vec());
215+
let mut jones = std::mem::ManuallyDrop::new(jones.into_raw_vec_and_offset().0);
216216

217217
let new_len = jones.len() * 4;
218218
let new_cap = jones.capacity() * 4;
219219
let new_ptr = jones.as_mut_ptr() as *mut GpuComplex;
220220
// SAFETY: new_cap == old_cap * N, align_of::<Complex>() == align_of::<Jones>()
221221
let flat = unsafe { Vec::from_raw_parts(new_ptr, new_len, new_cap) };
222222
let a4 = Array4::from_shape_vec((old_dim.0, old_dim.1, old_dim.2, 4), flat).unwrap();
223-
Ok(a4.into_pyarray(py))
223+
Ok(a4.into_pyarray_bound(py))
224224
}
225225
}

src/python/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use crate::fee::{FEEBeamError, InitFEEBeamError};
1616
/// A Python module interfacing with the hyperbeam code written in Rust. This
1717
/// module depends on and will import numpy.
1818
#[pymodule]
19-
fn mwa_hyperbeam(py: Python, m: &PyModule) -> PyResult<()> {
20-
py.import("numpy")?;
19+
fn mwa_hyperbeam(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
20+
py.import_bound("numpy")?;
2121
m.add_class::<fee::FEEBeam>()?;
2222
m.add_class::<analytic::AnalyticBeam>()?;
23-
m.add("HyperbeamError", py.get_type::<HyperbeamError>())?;
23+
m.add("HyperbeamError", py.get_type_bound::<HyperbeamError>())?;
2424
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
2525

2626
Ok(())

0 commit comments

Comments
 (0)