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

Shorthand filter_all argument for simplify #2681

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions python/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

**Features**

- A new ``filter_all`` parameter for simplify allows all filter_x params to be set
simultaneously (:user:`hyanwong`, :pr:`2681`, :issue:`2607`)

- A new ``Tree.is_root`` method avoids the need to to search the potentially
large list of ``Tree.roots`` (:user:`hyanwong`, :pr:`2669`, :issue:`2620`)

Expand Down
17 changes: 12 additions & 5 deletions python/tests/simplify.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2019-2022 Tskit Developers
# Copyright (c) 2019-2023 Tskit Developers
# Copyright (c) 2015-2018 University of Oxford
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -112,15 +112,22 @@ def __init__(
keep_unary_in_individuals=False,
keep_input_roots=False,
filter_nodes=True, # If this is False, the order in `sample` is ignored
filter_all=None, # Shorthand to set all other filter args at once
):
self.ts = ts
self.n = len(sample)
self.reduce_to_site_topology = reduce_to_site_topology
self.sequence_length = ts.sequence_length
self.filter_sites = filter_sites
self.filter_populations = filter_populations
self.filter_individuals = filter_individuals
self.filter_nodes = filter_nodes
if filter_all is not None:
self.filter_sites = filter_all
self.filter_populations = filter_all
self.filter_individuals = filter_all
self.filter_nodes = filter_all
else:
self.filter_sites = filter_sites
self.filter_populations = filter_populations
self.filter_individuals = filter_individuals
self.filter_nodes = filter_nodes
self.keep_unary = keep_unary
self.keep_unary_in_individuals = keep_unary_in_individuals
self.keep_input_roots = keep_input_roots
Expand Down
46 changes: 37 additions & 9 deletions python/tskit/tables.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# MIT License
#
# Copyright (c) 2018-2022 Tskit Developers
# Copyright (c) 2018-2023 Tskit Developers
# Copyright (c) 2017 University of Oxford
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -3349,6 +3349,7 @@ def simplify(
filter_individuals=None,
filter_sites=None,
filter_nodes=None,
filter_all=None,
keep_unary=False,
keep_unary_in_individuals=None,
keep_input_roots=False,
Expand Down Expand Up @@ -3416,6 +3417,13 @@ def simplify(
potential change to the node table may be to change the node flags
(if ``samples`` is specified and different from the existing samples).
(Default: None, treated as True)
:param bool filter_all: Shorthand for simultaneously setting all the ``filter_X``
parameters. Set to False for minimal simplification of the tree sequence,
such that the only altered tables are the edge table and potentially, the
flags in the node table (this only if the ``samples`` change, as described
in the ``filter_nodes`` parameter). If None, this parameter has no effect.
If any other value and any ``filter_X`` parameter has been set,
an error will be raised. (Default: None)
:param bool keep_unary: If True, preserve unary nodes (i.e. nodes with
exactly one child) that exist on the path from samples to root.
(Default: False)
Expand Down Expand Up @@ -3451,14 +3459,34 @@ def simplify(
].astype(np.int32)
else:
samples = util.safe_np_int_cast(samples, np.int32)
if filter_populations is None:
filter_populations = True
if filter_individuals is None:
filter_individuals = True
if filter_sites is None:
filter_sites = True
if filter_nodes is None:
filter_nodes = True
if filter_all is not None:
if any(
[
filter_x is not None
for filter_x in (
filter_populations,
filter_individuals,
filter_sites,
filter_nodes,
)
]
):
raise ValueError(
"Cannot combine `filter_all` with other `filter_X` parameters"
)
filter_populations = filter_all
filter_individuals = filter_all
filter_sites = filter_all
filter_nodes = filter_all
else:
if filter_populations is None:
filter_populations = True
if filter_individuals is None:
filter_individuals = True
if filter_sites is None:
filter_sites = True
if filter_nodes is None:
filter_nodes = True
if keep_unary_in_individuals is None:
keep_unary_in_individuals = False

Expand Down
9 changes: 9 additions & 0 deletions python/tskit/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -6516,6 +6516,7 @@ def simplify(
filter_individuals=None,
filter_sites=None,
filter_nodes=None,
filter_all=None,
keep_unary=False,
keep_unary_in_individuals=None,
keep_input_roots=False,
Expand Down Expand Up @@ -6589,6 +6590,13 @@ def simplify(
potential change to the node table may be to change the node flags
(if ``samples`` is specified and different from the existing samples).
(Default: None, treated as True)
:param bool filter_all: Shorthand for simultaneously setting all the ``filter_X``
parameters. Set to False for minimal simplification of the tree sequence,
such that the only altered tables are the edge table and potentially, the
flags in the node table (this only if the ``samples`` change, as described
in the ``filter_nodes`` parameter). If None, this parameter has no effect.
If any other value and any ``filter_X`` parameter has been set,
an error will be raised. (Default: None)
:param bool keep_unary: If True, preserve unary nodes (i.e., nodes with
exactly one child) that exist on the path from samples to root.
(Default: False)
Expand Down Expand Up @@ -6621,6 +6629,7 @@ def simplify(
filter_individuals=filter_individuals,
filter_sites=filter_sites,
filter_nodes=filter_nodes,
filter_all=filter_all,
keep_unary=keep_unary,
keep_unary_in_individuals=keep_unary_in_individuals,
keep_input_roots=keep_input_roots,
Expand Down