Skip to content

Commit

Permalink
Add new method Grid::delete_orders
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Aug 7, 2024
1 parent 4176a67 commit 8c12335
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- added new method `Grid::delete_orders` and the corresponding switch
`--delete-orders` in the subcommand `write` of the CLI

## [0.8.2] - 22/07/2024

### Changed
Expand Down
22 changes: 22 additions & 0 deletions pineappl/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,28 @@ impl Grid {
}
}

/// Delete orders with the corresponding `order_indices`. Repeated indices and indices larger
/// or equal than the number of orders are ignored.
pub fn delete_orders(&mut self, order_indices: &[usize]) {
let mut order_indices: Vec<_> = order_indices
.iter()
.copied()
// ignore indices corresponding to orders that don't exist
.filter(|&index| index < self.orders().len())
.collect();

// sort and remove repeated indices
order_indices.sort_unstable();
order_indices.dedup();
order_indices.reverse();
let order_indices = order_indices;

for index in order_indices {
self.orders.remove(index);
self.subgrids.remove_index(Axis(0), index);
}
}

pub(crate) fn rewrite_channels(&mut self, add: &[(i32, i32)], del: &[i32]) {
self.channels = self
.channels()
Expand Down
17 changes: 16 additions & 1 deletion pineappl_cli/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ enum OpsArg {
DedupChannels(i64),
DeleteBins(Vec<RangeInclusive<usize>>),
DeleteChannels(Vec<RangeInclusive<usize>>),
DeleteOrders(Vec<RangeInclusive<usize>>),
DeleteKey(String),
MergeBins(Vec<RangeInclusive<usize>>),
Optimize(bool),
Expand Down Expand Up @@ -134,7 +135,7 @@ impl FromArgMatches for MoreArgs {
});
}
}
"delete_bins" | "delete_channels" | "merge_bins" => {
"delete_bins" | "delete_channels" | "delete_orders" | "merge_bins" => {
for (index, arg) in indices.into_iter().zip(
matches
.remove_occurrences(&id)
Expand All @@ -144,6 +145,7 @@ impl FromArgMatches for MoreArgs {
args[index] = Some(match id.as_str() {
"delete_bins" => OpsArg::DeleteBins(arg),
"delete_channels" => OpsArg::DeleteChannels(arg),
"delete_orders" => OpsArg::DeleteOrders(arg),
"merge_bins" => OpsArg::MergeBins(arg),
_ => unreachable!(),
});
Expand Down Expand Up @@ -315,6 +317,16 @@ impl Args for MoreArgs {
.value_name("CH1-CH2,...")
.value_parser(helpers::parse_integer_range),
)
.arg(
Arg::new("delete_orders")
.action(ArgAction::Append)
.help("Delete orders with the specified indices")
.long("delete-orders")
.num_args(1)
.value_delimiter(',')
.value_name("O1-O2,...")
.value_parser(helpers::parse_integer_range),
)
.arg(
Arg::new("delete_key")
.action(ArgAction::Append)
Expand Down Expand Up @@ -539,6 +551,9 @@ impl Subcommand for Opts {
OpsArg::DeleteChannels(ranges) => {
grid.delete_channels(&ranges.iter().flat_map(Clone::clone).collect::<Vec<_>>());
}
OpsArg::DeleteOrders(ranges) => {
grid.delete_orders(&ranges.iter().flat_map(Clone::clone).collect::<Vec<_>>());
}
OpsArg::DeleteKey(key) => {
grid.key_values_mut().remove(key);
}
Expand Down
31 changes: 31 additions & 0 deletions pineappl_cli/tests/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Options:
--dedup-channels[=<ULPS>] Deduplicate channels assuming numbers differing by ULPS are the same
--delete-bins <BIN1-BIN2,...> Delete bins with the specified indices
--delete-channels <CH1-CH2,...> Delete channels with the specified indices
--delete-orders <O1-O2,...> Delete orders with the specified indices
--delete-key <KEY> Delete an internal key-value pair
--merge-bins <BIN1-BIN2,...> Merge specific bins together
--optimize[=<ENABLE>] Optimize internal data structure to minimize memory and disk usage [possible values: true, false]
Expand Down Expand Up @@ -437,6 +438,12 @@ const REWRITE_ORDER_READ_STR: &str = "o order
4 O(a^3 lf^1)
";

const DELETE_ORDERS_STR: &str = "o order
-+----------------
0 O(as^1 a^2)
1 O(as^1 a^2 lf^1)
";

#[test]
fn help() {
Command::cargo_bin("pineappl")
Expand Down Expand Up @@ -1123,3 +1130,27 @@ fn rewrite_order() {
.success()
.stdout(REWRITE_ORDER_CONVOLVE_STR);
}

#[test]
fn delete_orders() {
let output = NamedTempFile::new("deleted4.pineappl.lz4").unwrap();

Command::cargo_bin("pineappl")
.unwrap()
.args([
"write",
"--delete-orders=3-4,0",
"../test-data/LHCB_WP_7TEV_opt.pineappl.lz4",
output.path().to_str().unwrap(),
])
.assert()
.success()
.stdout("");

Command::cargo_bin("pineappl")
.unwrap()
.args(["read", "--orders", output.path().to_str().unwrap()])
.assert()
.success()
.stdout(DELETE_ORDERS_STR);
}

0 comments on commit 8c12335

Please sign in to comment.