Skip to content

Commit 8c12335

Browse files
committed
Add new method Grid::delete_orders
1 parent 4176a67 commit 8c12335

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- added new method `Grid::delete_orders` and the corresponding switch
13+
`--delete-orders` in the subcommand `write` of the CLI
14+
1015
## [0.8.2] - 22/07/2024
1116

1217
### Changed

pineappl/src/grid.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,28 @@ impl Grid {
17521752
}
17531753
}
17541754

1755+
/// Delete orders with the corresponding `order_indices`. Repeated indices and indices larger
1756+
/// or equal than the number of orders are ignored.
1757+
pub fn delete_orders(&mut self, order_indices: &[usize]) {
1758+
let mut order_indices: Vec<_> = order_indices
1759+
.iter()
1760+
.copied()
1761+
// ignore indices corresponding to orders that don't exist
1762+
.filter(|&index| index < self.orders().len())
1763+
.collect();
1764+
1765+
// sort and remove repeated indices
1766+
order_indices.sort_unstable();
1767+
order_indices.dedup();
1768+
order_indices.reverse();
1769+
let order_indices = order_indices;
1770+
1771+
for index in order_indices {
1772+
self.orders.remove(index);
1773+
self.subgrids.remove_index(Axis(0), index);
1774+
}
1775+
}
1776+
17551777
pub(crate) fn rewrite_channels(&mut self, add: &[(i32, i32)], del: &[i32]) {
17561778
self.channels = self
17571779
.channels()

pineappl_cli/src/write.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum OpsArg {
3535
DedupChannels(i64),
3636
DeleteBins(Vec<RangeInclusive<usize>>),
3737
DeleteChannels(Vec<RangeInclusive<usize>>),
38+
DeleteOrders(Vec<RangeInclusive<usize>>),
3839
DeleteKey(String),
3940
MergeBins(Vec<RangeInclusive<usize>>),
4041
Optimize(bool),
@@ -134,7 +135,7 @@ impl FromArgMatches for MoreArgs {
134135
});
135136
}
136137
}
137-
"delete_bins" | "delete_channels" | "merge_bins" => {
138+
"delete_bins" | "delete_channels" | "delete_orders" | "merge_bins" => {
138139
for (index, arg) in indices.into_iter().zip(
139140
matches
140141
.remove_occurrences(&id)
@@ -144,6 +145,7 @@ impl FromArgMatches for MoreArgs {
144145
args[index] = Some(match id.as_str() {
145146
"delete_bins" => OpsArg::DeleteBins(arg),
146147
"delete_channels" => OpsArg::DeleteChannels(arg),
148+
"delete_orders" => OpsArg::DeleteOrders(arg),
147149
"merge_bins" => OpsArg::MergeBins(arg),
148150
_ => unreachable!(),
149151
});
@@ -315,6 +317,16 @@ impl Args for MoreArgs {
315317
.value_name("CH1-CH2,...")
316318
.value_parser(helpers::parse_integer_range),
317319
)
320+
.arg(
321+
Arg::new("delete_orders")
322+
.action(ArgAction::Append)
323+
.help("Delete orders with the specified indices")
324+
.long("delete-orders")
325+
.num_args(1)
326+
.value_delimiter(',')
327+
.value_name("O1-O2,...")
328+
.value_parser(helpers::parse_integer_range),
329+
)
318330
.arg(
319331
Arg::new("delete_key")
320332
.action(ArgAction::Append)
@@ -539,6 +551,9 @@ impl Subcommand for Opts {
539551
OpsArg::DeleteChannels(ranges) => {
540552
grid.delete_channels(&ranges.iter().flat_map(Clone::clone).collect::<Vec<_>>());
541553
}
554+
OpsArg::DeleteOrders(ranges) => {
555+
grid.delete_orders(&ranges.iter().flat_map(Clone::clone).collect::<Vec<_>>());
556+
}
542557
OpsArg::DeleteKey(key) => {
543558
grid.key_values_mut().remove(key);
544559
}

pineappl_cli/tests/write.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Options:
1515
--dedup-channels[=<ULPS>] Deduplicate channels assuming numbers differing by ULPS are the same
1616
--delete-bins <BIN1-BIN2,...> Delete bins with the specified indices
1717
--delete-channels <CH1-CH2,...> Delete channels with the specified indices
18+
--delete-orders <O1-O2,...> Delete orders with the specified indices
1819
--delete-key <KEY> Delete an internal key-value pair
1920
--merge-bins <BIN1-BIN2,...> Merge specific bins together
2021
--optimize[=<ENABLE>] Optimize internal data structure to minimize memory and disk usage [possible values: true, false]
@@ -437,6 +438,12 @@ const REWRITE_ORDER_READ_STR: &str = "o order
437438
4 O(a^3 lf^1)
438439
";
439440

441+
const DELETE_ORDERS_STR: &str = "o order
442+
-+----------------
443+
0 O(as^1 a^2)
444+
1 O(as^1 a^2 lf^1)
445+
";
446+
440447
#[test]
441448
fn help() {
442449
Command::cargo_bin("pineappl")
@@ -1123,3 +1130,27 @@ fn rewrite_order() {
11231130
.success()
11241131
.stdout(REWRITE_ORDER_CONVOLVE_STR);
11251132
}
1133+
1134+
#[test]
1135+
fn delete_orders() {
1136+
let output = NamedTempFile::new("deleted4.pineappl.lz4").unwrap();
1137+
1138+
Command::cargo_bin("pineappl")
1139+
.unwrap()
1140+
.args([
1141+
"write",
1142+
"--delete-orders=3-4,0",
1143+
"../test-data/LHCB_WP_7TEV_opt.pineappl.lz4",
1144+
output.path().to_str().unwrap(),
1145+
])
1146+
.assert()
1147+
.success()
1148+
.stdout("");
1149+
1150+
Command::cargo_bin("pineappl")
1151+
.unwrap()
1152+
.args(["read", "--orders", output.path().to_str().unwrap()])
1153+
.assert()
1154+
.success()
1155+
.stdout(DELETE_ORDERS_STR);
1156+
}

0 commit comments

Comments
 (0)