From 5acae8ffde701ee992d708e2273cb554e804d142 Mon Sep 17 00:00:00 2001 From: Emilia Date: Sat, 18 Jan 2025 12:47:32 -0500 Subject: [PATCH] transfer_region refactor 1 Cleaning up difficult to reason about code --- plate-tool-lib/src/transfer_region.rs | 95 ++++++++++++++------------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/plate-tool-lib/src/transfer_region.rs b/plate-tool-lib/src/transfer_region.rs index e33a264..9aa0b03 100644 --- a/plate-tool-lib/src/transfer_region.rs +++ b/plate-tool-lib/src/transfer_region.rs @@ -312,31 +312,16 @@ impl TransferRegion { .div_euclid(il_source.1.unsigned_abs()), ); let count = ( - // How many times can we replicate? - if il_dest.0.unsigned_abs() == 0 { - 1 - } else { - (1..) - .position(|n| { - (n * number_used_src_wells.0 * il_dest.0.unsigned_abs()) - .saturating_sub(il_dest.0.unsigned_abs()) - + 1 - > d_dims.0 - }) - .unwrap() as u8 - }, - if il_dest.1.unsigned_abs() == 0 { - 1 - } else { - (1..) - .position(|n| { - (n * number_used_src_wells.1 * il_dest.1.unsigned_abs()) - .saturating_sub(il_dest.1.unsigned_abs()) - + 1 - > d_dims.1 - }) - .unwrap() as u8 - }, + Self::calculate_replication_count( + number_used_src_wells.0, + il_dest.0, + d_dims.0, + ), + Self::calculate_replication_count( + number_used_src_wells.1, + il_dest.1, + d_dims.1, + ), ); let i = i .saturating_sub(s_ul.row) @@ -354,27 +339,25 @@ impl TransferRegion { Some( possible_destination_wells .into_iter() - .filter(|Well { row: x, .. }| { - x.checked_sub(d_ul.row).unwrap() - % row_modulus // Counter along x - == (il_dest.0.unsigned_abs() *i) - % row_modulus - }) - .filter(|Well { col: y, .. }| { - y.checked_sub(d_ul.col).unwrap() - % column_modulus // Counter along u - == (il_dest.1.unsigned_abs() *j) - % column_modulus - }) .filter(|Well { row: x, col: y }| { - // How many times have we replicated? < How many are we allowed - // to replicate? - x.checked_sub(d_ul.row).unwrap().div_euclid(row_modulus) + // Compute row and column offsets once + let row_offset = x.checked_sub(d_ul.row).unwrap(); + let col_offset = y.checked_sub(d_ul.col).unwrap(); + + // Row position check + let row_matches = row_offset % row_modulus + == (il_dest.0.unsigned_abs() * i) % row_modulus; + + // Column position check + let col_matches = col_offset % column_modulus + == (il_dest.1.unsigned_abs() * j) % column_modulus; + + // Replication limits check + let within_limits = row_offset.div_euclid(row_modulus) < count.0 - && y.checked_sub(d_ul.col) - .unwrap() - .div_euclid(column_modulus) - < count.1 + && col_offset.div_euclid(column_modulus) < count.1; + + row_matches && col_matches && within_limits }) .collect(), ) @@ -429,6 +412,30 @@ impl TransferRegion { } } + fn calculate_replication_count( + number_of_source_wells: u8, + dest_interleave: i8, + dest_dimension: u8, + ) -> u8 { + if dest_interleave == 0 { + return 1; + } + + // Find the first position where the wells won't fit + // This tells us how many times we can replicate + (1..) + .position(|n| { + let wells_needed = { + // Calculate total wells needed for n replications + let total_wells = n * number_of_source_wells * dest_interleave.unsigned_abs(); + // Adjust for interleaving offset and convert to actual position + total_wells.saturating_sub(dest_interleave.unsigned_abs()) + 1 + }; + wells_needed > dest_dimension + }) + .unwrap() as u8 + } + pub fn validate(&self) -> Result<(), &'static str> { // Checks if the region does anything suspect //