1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! CorrelatorContext methods for Python
#[cfg(any(feature = "python", feature = "python-stubgen"))]
use super::*;
#[cfg(any(feature = "python", feature = "python-stubgen"))]
use ndarray::Array2;
#[cfg(any(feature = "python", feature = "python-stubgen"))]
use ndarray::Array3;
#[cfg(any(feature = "python", feature = "python-stubgen"))]
use numpy::PyArray2;
#[cfg(any(feature = "python", feature = "python-stubgen"))]
use numpy::PyArray3;
#[cfg(feature = "python-stubgen")]
use pyo3_stub_gen_derive::gen_stub_pymethods;
#[cfg_attr(feature = "python-stubgen", gen_stub_pymethods)]
#[cfg_attr(feature = "python", pymethods)]
#[cfg(any(feature = "python", feature = "python-stubgen"))]
impl CorrelatorContext {
/// From a path to a metafits file and paths to gpubox files, create a `CorrelatorContext`.
///
/// Args:
/// metafits_filename (str): filename of metafits file as a path or string.
/// gpubox_filenames (list[str]): list of filenames of gpubox files.
///
/// Returns:
/// correlator_context (CorelatorContext): a populated CorrelatorContext object if Ok.
#[new]
#[pyo3(signature = (metafits_filename, gpubox_filenames), text_signature = "(metafits_filename: str, mwa_version: list[gpubox_filenames])")]
fn pyo3_new(metafits_filename: &str, gpubox_filenames: Vec<String>) -> PyResult<Self> {
// Convert the gpubox filenames.
let gpubox_filenames: Vec<String> = gpubox_filenames
.into_iter()
.map(|g| g.to_string())
.collect();
let c = CorrelatorContext::new(metafits_filename, &gpubox_filenames)?;
Ok(c)
}
/// For a given list of correlator coarse channel indices, return a list of the center frequencies for all the fine channels in the given coarse channels
///
/// Args:
/// corr_coarse_chan_indices (list[int]): a list containing correlator coarse channel indices for which you want fine channels for. Does not need to be contiguous.
///
/// Returns:
/// fine_chan_freqs_hz_array (list[float]): a vector of floats containing the centre sky frequencies of all the fine channels for the given coarse channels.
#[pyo3(name = "get_fine_chan_freqs_hz_array")]
fn pyo3_get_fine_chan_freqs_hz_array(&self, corr_coarse_chan_indices: Vec<usize>) -> Vec<f64> {
self.get_fine_chan_freqs_hz_array(&corr_coarse_chan_indices)
}
/// Read a single timestep for a single coarse channel. The output visibilities are in order: baseline,frequency,pol,r,i
///
/// Args:
/// corr_timestep_index (int): index within the CorrelatorContext timestep array for the desired timestep. This corresponds to the element within CorrelatorContext.timesteps.
/// corr_coarse_chan_index (int): index within the CorrelatorContext coarse_chan array for the desired coarse channel. This corresponds to the element within CorrelatorContext.coarse_chans.
///
/// Returns:
/// data (numpy.typing.NDArray[numpy.float32]): 3 dimensional ndarray of 32 bit floats containing the data in [baseline],[frequency],[pol,r,i] order, if Ok.
#[pyo3(
name = "read_by_baseline",
text_signature = "(self, corr_timestep_index, corr_coarse_chan_index)"
)]
fn pyo3_read_by_baseline<'py>(
&self,
py: Python<'py>,
corr_timestep_index: usize,
corr_coarse_chan_index: usize,
) -> PyResult<Bound<'py, PyArray3<f32>>> {
// Use the existing Rust method.
let data = self.read_by_baseline(corr_timestep_index, corr_coarse_chan_index)?;
// Convert the vector to a 3D array (this is free).
let data = Array3::from_shape_vec(
(
self.metafits_context.num_baselines,
self.metafits_context.num_corr_fine_chans_per_coarse,
8,
),
data,
)
.expect("shape of data should match expected dimensions (num_baselines, num_corr_fine_chans_per_coarse, visibility_pols * 2)");
// Convert to a numpy array.
let data = PyArray3::from_owned_array(py, data);
Ok(data)
}
/// Read a single timestep for a single coarse channel. The output visibilities are in order: frequency,baseline,pol,r,i
///
/// Args:
/// corr_timestep_index (int): index within the CorrelatorContext timestep array for the desired timestep. This corresponds to the element within CorrelatorContext.timesteps.
/// corr_coarse_chan_index (int): index within the CorrelatorContext coarse_chan array for the desired coarse channel. This corresponds to the element within CorrelatorContext.coarse_chans.
///
/// Returns:
/// data (numpy.typing.NDArray[numpy.float32]): 3 dimensional ndarray of 32 bit floats containing the data in [frequency],[baseline],[pol,r,i] order, if Ok.
#[pyo3(
name = "read_by_frequency",
text_signature = "(self, corr_timestep_index, corr_coarse_chan_index)"
)]
fn pyo3_read_by_frequency<'py>(
&self,
py: Python<'py>,
corr_timestep_index: usize,
corr_coarse_chan_index: usize,
) -> PyResult<Bound<'py, PyArray3<f32>>> {
// Use the existing Rust method.
let data = self.read_by_frequency(corr_timestep_index, corr_coarse_chan_index)?;
// Convert the vector to a 3D array (this is free).
let data = Array3::from_shape_vec(
(
self.metafits_context.num_corr_fine_chans_per_coarse,
self.metafits_context.num_baselines,
8,
),
data,
)
.expect("shape of data should match expected dimensions (num_corr_fine_chans_per_coarse, num_baselines, visibility_pols * 2)");
// Convert to a numpy array.
let data = PyArray3::from_owned_array(py, data);
Ok(data)
}
/// Read weights for a single timestep for a single coarse channel. The output weights are in order: baseline,pol
///
/// Args:
/// corr_timestep_index (int): index within the CorrelatorContext timestep array for the desired timestep. This corresponds to the element within CorrelatorContext.timesteps.
/// corr_coarse_chan_index (int): index within the CorrelatorContext coarse_chan array for the desired coarse channel. This corresponds to the element within CorrelatorContext.coarse_chans.
///
/// Returns:
/// data (numpy.typing.NDArray[numpy.float32]): A 2 dimensional ndarray of 32 bit floats containing the data in [baseline],[pol] order, if Ok.
#[pyo3(
name = "read_weights_by_baseline",
text_signature = "(self, corr_timestep_index, corr_coarse_chan_index)"
)]
fn pyo3_read_weights_by_baseline<'py>(
&self,
py: Python<'py>,
corr_timestep_index: usize,
corr_coarse_chan_index: usize,
) -> PyResult<Bound<'py, PyArray2<f32>>> {
// Use the existing Rust method.
let data = self.read_weights_by_baseline(corr_timestep_index, corr_coarse_chan_index)?;
// Convert the vector to a 3D array (this is free).
let data = Array2::from_shape_vec(
(
self.metafits_context.num_baselines,
self.metafits_context.num_visibility_pols,
),
data,
)
.expect("shape of data should match expected dimensions (num_baselines, visibility_pols)");
// Convert to a numpy array.
let data = PyArray2::from_owned_array(py, data);
Ok(data)
}
// https://pyo3.rs/v0.17.3/class/object.html#string-representations
fn __repr__(&self) -> String {
format!("{}", self)
}
#[pyo3()]
fn __enter__(slf: Py<Self>) -> Py<Self> {
slf
}
fn __exit__(
&mut self,
_exc_type: &Bound<PyAny>,
_exc_value: &Bound<PyAny>,
_traceback: &Bound<PyAny>,
) {
}
}