Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement antenna (tile) selection (--sel-ants) #151

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ ndarray = { version = "0.15.4", features = ["approx-0_5"] }
tempfile = "3.3"

[build-dependencies]
built = { version = "~0.7.3", default-features = false, features = [
built = { version = "~0.7", default-features = false, features = [
"chrono",
"git2",
] }
Expand All @@ -91,6 +91,6 @@ opt-level = 3

[patch.crates-io]
# marlu = { path = "../Marlu" }
# marlu = { git = "https://github.com/MWATelescope/Marlu", branch = "DUT1" }
# marlu = { git = "https://github.com/MWATelescope/Marlu", branch = "birli-150" }
# mwalib = { path = "../mwalib" }
# mwalib = { git = "https://github.com/MWATelescope/mwalib", branch = "digital_gains_plus" }
8 changes: 4 additions & 4 deletions benches/expensive_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ fn bench_correct_cable_lengths_mwax_half_1247842824(crt: &mut Criterion) {
black_box(&corr_ctx),
black_box(jones_array.view_mut()),
black_box(&vis_sel.coarse_chan_range),
black_box(&vis_sel.baseline_idxs),
false,
)
})
Expand Down Expand Up @@ -147,6 +148,7 @@ fn bench_correct_cable_lengths_ord_half_1196175296(crt: &mut Criterion) {
black_box(&corr_ctx),
black_box(jones_array.view_mut()),
black_box(&vis_sel.coarse_chan_range),
black_box(&vis_sel.baseline_idxs),
false,
)
})
Expand All @@ -173,8 +175,7 @@ fn bench_correct_geometry_mwax_half_1247842824(crt: &mut Criterion) {
correct_geometry(
black_box(&corr_ctx),
black_box(jones_array.view_mut()),
black_box(&vis_sel.timestep_range),
black_box(&vis_sel.coarse_chan_range),
black_box(&vis_sel),
None,
None,
false,
Expand Down Expand Up @@ -203,8 +204,7 @@ fn bench_correct_geometry_ord_half_1196175296(crt: &mut Criterion) {
correct_geometry(
black_box(&corr_ctx),
black_box(jones_array.view_mut()),
black_box(&vis_sel.timestep_range),
black_box(&vis_sel.coarse_chan_range),
black_box(&vis_sel),
None,
None,
false,
Expand Down
52 changes: 50 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1841,8 +1841,8 @@ mod tests {
use tempfile::tempdir;

use crate::{
test_common::get_1254670392_avg_paths, test_common::get_mwax_data_paths, BirliContext,
VisSelection,
test_common::{get_1254670392_avg_paths, get_mwax_data_paths},
BirliContext, BirliError, VisSelection,
};

#[test]
Expand Down Expand Up @@ -2143,6 +2143,54 @@ mod tests {

let BirliContext { flag_ctx, .. } = BirliContext::from_args(&args).unwrap();
}

/// Test that corrections work correctly with `--sel-ants`
#[test]
fn test_sel_ants_baselines() {
let (metafits_path, gpufits_paths) = get_1254670392_avg_paths();

#[rustfmt::skip]
let args = vec![
"birli",
"-m", metafits_path,
"--sel-ants", "1", "2", "3", "4",
"--no-draw-progress",
gpufits_paths[0],
gpufits_paths[1],
];

let birli_ctx = BirliContext::from_args(&args).unwrap();

// check baseline_idxs is the correct size
assert_eq!(&birli_ctx.vis_sel.baseline_idxs.len(), &10);
assert_eq!(
&birli_ctx.vis_sel.baseline_idxs,
&[128, 129, 130, 131, 255, 256, 257, 381, 382, 506]
);

birli_ctx.run().unwrap();
}

/// Test `--sel-ants` handles invalid antenna idxs
#[test]
fn test_sel_ants_invalid() {
let (metafits_path, gpufits_paths) = get_1254670392_avg_paths();

#[rustfmt::skip]
let args = vec![
"birli",
"-m", metafits_path,
"--sel-ants", "0", "11", "3", "999",
"--no-draw-progress",
gpufits_paths[0],
gpufits_paths[1],
];

assert!(matches!(
BirliContext::from_args(&args),
Err(BirliError::CLIError(_))
));
}
}

#[cfg(test)]
Expand Down
69 changes: 33 additions & 36 deletions src/corrections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use marlu::{
io::error::BadArrayShape,
mwalib::{CorrelatorContext, MWAVersion},
precession::precess_time,
Complex, LatLngHeight, RADec, XyzGeodetic, UVW,
Complex, LatLngHeight, RADec, VisSelection, XyzGeodetic, UVW,
};
use std::{f64::consts::TAU, ops::Range};
use thiserror::Error;
Expand Down Expand Up @@ -55,7 +55,7 @@ use thiserror::Error;
/// read_mwalib(&vis_sel, &corr_ctx, jones_array.view_mut(), flag_array.view_mut(), false)
/// .unwrap();
///
/// correct_cable_lengths(&corr_ctx, jones_array.view_mut(), &vis_sel.coarse_chan_range, false);
/// correct_cable_lengths(&corr_ctx, jones_array.view_mut(), &vis_sel.coarse_chan_range, &vis_sel.baseline_idxs, false);
/// ```
///
/// # Accuracy
Expand All @@ -65,9 +65,9 @@ use thiserror::Error;
pub fn correct_cable_lengths(
corr_ctx: &CorrelatorContext,
mut jones_array: ArrayViewMut3<Jones<f32>>,
// TODO: take a VisSelection
coarse_chan_range: &Range<usize>,
// TODO: allow subset of baselines
// baseline_idxs: &[usize],
baseline_idxs: &[usize],
draw_progress: bool,
) {
trace!("start correct_cable_lengths");
Expand All @@ -77,9 +77,14 @@ pub fn correct_cable_lengths(
let all_freqs_hz =
corr_ctx.get_fine_chan_freqs_hz_array(&coarse_chan_range.clone().collect::<Vec<_>>());

let ant_pairs = (meta_ctx.baselines)
let ant_pairs = baseline_idxs
.iter()
.map(|b| (b.ant1_index, b.ant2_index))
.map(|b| {
(
meta_ctx.baselines[*b].ant1_index,
meta_ctx.baselines[*b].ant2_index,
)
})
.collect::<Vec<_>>();

let draw_target = if draw_progress {
Expand Down Expand Up @@ -188,26 +193,21 @@ pub fn correct_cable_lengths(
/// read_mwalib(&vis_sel, &corr_ctx, jones_array.view_mut(), flag_array.view_mut(), false)
/// .unwrap();
///
/// correct_cable_lengths(&corr_ctx, jones_array.view_mut(), &vis_sel.coarse_chan_range, false);
/// correct_cable_lengths(&corr_ctx, jones_array.view_mut(), &vis_sel.coarse_chan_range, &vis_sel.baseline_idxs, false);
///
/// correct_geometry(
/// &corr_ctx,
/// jones_array.view_mut(),
/// &vis_sel.timestep_range,
/// &vis_sel.coarse_chan_range,
/// &vis_sel,
/// None,
/// None,
/// false,
/// );
/// ```
#[allow(clippy::too_many_arguments)]
pub fn correct_geometry(
corr_ctx: &CorrelatorContext,
mut jones_array: ArrayViewMut3<Jones<f32>>,
timestep_range: &Range<usize>,
coarse_chan_range: &Range<usize>,
// TODO: allow subset of baselines
// baseline_idxs: &[usize],
vis_sel: &VisSelection,
array_pos: Option<LatLngHeight>,
phase_centre: Option<RADec>,
draw_progress: bool,
Expand All @@ -222,12 +222,10 @@ pub fn correct_geometry(
LatLngHeight::mwa()
});

let timesteps = &corr_ctx.timesteps[timestep_range.clone()];

let baselines = &corr_ctx.metafits_context.baselines;
let timesteps = &corr_ctx.timesteps[vis_sel.timestep_range.clone()];

let all_freqs_hz =
corr_ctx.get_fine_chan_freqs_hz_array(&coarse_chan_range.clone().collect::<Vec<_>>());
let all_freqs_hz = corr_ctx
.get_fine_chan_freqs_hz_array(&vis_sel.coarse_chan_range.clone().collect::<Vec<_>>());
let jones_dims = jones_array.dim();

let integration_time_s = corr_ctx.metafits_context.corr_int_time_ms as f64 / 1000.0;
Expand All @@ -236,17 +234,14 @@ pub fn correct_geometry(
.unwrap_or_else(|| RADec::from_mwalib_phase_or_pointing(&corr_ctx.metafits_context));
let tiles_xyz_geod = XyzGeodetic::get_tiles(&corr_ctx.metafits_context, array_pos.latitude_rad);

let ant_pairs = baselines
.iter()
.map(|b| (b.ant1_index, b.ant2_index))
.collect::<Vec<_>>();
// use baseline_idxs to select antpairs out of corr_ctx.metafits_context.baselines
let ant_pairs = vis_sel.get_ant_pairs(&corr_ctx.metafits_context);
let centroid_timestamps = timesteps
.iter()
.map(|t| Epoch::from_gpst_seconds(t.gps_time_ms as f64 / 1000.0 + integration_time_s / 2.0))
.collect::<Vec<_>>();
let dut1 = Duration::from_seconds(corr_ctx.metafits_context.dut1.unwrap_or(0.0));
let part_uvws = calc_part_uvws(
&ant_pairs,
&centroid_timestamps,
dut1,
phase_centre,
Expand Down Expand Up @@ -329,9 +324,9 @@ pub enum DigitalGainCorrection {
pub fn correct_digital_gains(
corr_ctx: &CorrelatorContext,
jones_array: ArrayViewMut3<Jones<f32>>,
// TODO: take a VisSelection
coarse_chan_range: &Range<usize>,
ant_pairs: &[(usize, usize)],
// TODO: take a VisSelection
) -> Result<(), DigitalGainCorrection> {
let num_fine_chans_per_coarse = corr_ctx.metafits_context.num_corr_fine_chans_per_coarse;

Expand Down Expand Up @@ -656,15 +651,14 @@ pub fn scrunch_gains(
// UVWs are in units of meters. To get the UVWs in units of wavelengths, divide by the wavelength.
// uvw at ts, (ant1, ant2) = part_uvw[ant1] - part_uvw[ant2]
fn calc_part_uvws(
ant_pairs: &[(usize, usize)],
centroid_timestamps: &[Epoch],
dut1: Duration,
phase_centre: RADec,
array_pos: LatLngHeight,
tile_xyzs: &[XyzGeodetic],
) -> Array2<UVW> {
let max_ant = ant_pairs.iter().map(|&(a, b)| a.max(b)).max().unwrap();
let mut part_uvws = Array2::from_elem((centroid_timestamps.len(), max_ant + 1), UVW::default());
let nants = tile_xyzs.len();
let mut part_uvws = Array2::from_elem((centroid_timestamps.len(), nants), UVW::default());
for (t, &epoch) in centroid_timestamps.iter().enumerate() {
let prec = precess_time(
array_pos.longitude_rad,
Expand Down Expand Up @@ -712,7 +706,8 @@ mod tests {
#[test]
fn test_cable_length_corrections_mwax() {
let corr_ctx = get_mwax_context();
let vis_sel = VisSelection::from_mwalib(&corr_ctx).unwrap();
let mut vis_sel = VisSelection::from_mwalib(&corr_ctx).unwrap();
vis_sel.baseline_idxs = vec![0, 1];

// Create a blank array to store flags and visibilities
let fine_chans_per_coarse = corr_ctx.metafits_context.num_corr_fine_chans_per_coarse;
Expand Down Expand Up @@ -814,6 +809,7 @@ mod tests {
&corr_ctx,
jones_array.view_mut(),
&vis_sel.coarse_chan_range,
&vis_sel.baseline_idxs,
false,
);

Expand Down Expand Up @@ -954,6 +950,7 @@ mod tests {
&corr_ctx,
jones_array.view_mut(),
&vis_sel.coarse_chan_range,
&vis_sel.baseline_idxs,
false,
);

Expand Down Expand Up @@ -1108,8 +1105,7 @@ mod tests {
correct_geometry(
&corr_ctx,
jones_array.view_mut(),
&vis_sel.timestep_range,
&vis_sel.coarse_chan_range,
&vis_sel,
None,
None,
false,
Expand Down Expand Up @@ -1139,7 +1135,9 @@ mod tests {
#[test]
fn test_geometric_corrections_mwax() {
let corr_ctx = get_mwax_context();
let vis_sel = VisSelection::from_mwalib(&corr_ctx).unwrap();
let mut vis_sel = VisSelection::from_mwalib(&corr_ctx).unwrap();
// only select baselines we're looking at
vis_sel.baseline_idxs = vec![0, 1];

// Create a blank array to store flags and visibilities
let fine_chans_per_coarse = corr_ctx.metafits_context.num_corr_fine_chans_per_coarse;
Expand Down Expand Up @@ -1177,7 +1175,7 @@ mod tests {
])
);

// ts 0, chan 0 (cc 0, fc 0), baseline 5
// ts 0, chan 0 (cc 0, fc 0), baseline 1
let viz_0_0_1 = jones_array[(0, 0, 1)];
compare_jones!(
viz_0_0_1,
Expand All @@ -1189,7 +1187,7 @@ mod tests {
])
);

// ts 3, chan 3 (cc 1, fc 1), baseline 5
// ts 3, chan 3 (cc 1, fc 1), baseline 1
let viz_3_3_1 = jones_array[(3, 3, 1)];
compare_jones!(
viz_3_3_1,
Expand Down Expand Up @@ -1256,8 +1254,7 @@ mod tests {
correct_geometry(
&corr_ctx,
jones_array.view_mut(),
&vis_sel.timestep_range,
&vis_sel.coarse_chan_range,
&vis_sel,
None,
None,
false,
Expand Down
Loading
Loading