Skip to content

Commit

Permalink
Merge pull request #233 from petrelharp/samplestime
Browse files Browse the repository at this point in the history
A few more fixes.
  • Loading branch information
petrelharp committed Dec 20, 2021
2 parents 48fa2c0 + 28e0b69 commit 96803a5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
24 changes: 13 additions & 11 deletions .github/workflows/tests.yml
Expand Up @@ -36,9 +36,11 @@ jobs:
CACHE_NUM: 2
with:
path: |
${{ steps.find-conda.outputs.CONDA }}/envs/${{ env.CONDA_ENV_NAME }}
~/.bash_profile
key: ${{ runner.os }}-${{ matrix.python}}-conda-v10-${{ hashFiles('requirements/CI/pip-requirements.txt') }}-${{ hashFiles('requirements/CI/conda-requirements.txt') }}-${{ env.CACHE_NUM }}
c:\Miniconda\envs\anaconda-client-env
/usr/share/miniconda/envs/anaconda-client-env
~/osx-conda
~/.profile
key: ${{ runner.os }}-${{ matrix.python}}-conda-v${{ env.CACHE_NUM }}-${{ hashFiles('requirements/CI/pip-requirements.txt') }}-${{ hashFiles('requirements/CI/conda-requirements.txt') }}

- name: Install Conda
uses: conda-incubator/setup-miniconda@v2
Expand Down Expand Up @@ -81,14 +83,14 @@ jobs:
mkdir -p /usr/local/miniconda/envs
sudo cp -r ~/osx-conda /usr/local/miniconda/envs/anaconda-client-env
# Installing SLiM from conda now.
# - name: Build SLiM
# run: |
# git clone https://github.com/messerlab/SLiM.git
# mkdir -p SLiM/Release
# cd SLiM/Release
# cmake -D CMAKE_BUILD_TYPE=Release ..
# make -j 2
# Installing SLiM from conda now.
# - name: Build SLiM
# run: |
# git clone https://github.com/messerlab/SLiM.git
# mkdir -p SLiM/Release
# cd SLiM/Release
# cmake -D CMAKE_BUILD_TYPE=Release ..
# make -j 2

- name: Run tests
run: |
Expand Down
9 changes: 6 additions & 3 deletions docs/development.md
Expand Up @@ -35,12 +35,12 @@ python -m pytest tests

If you would like to add some features to ``pyslim``, please read the
following. If you think there is anything missing,
please open an `issue <http://github.com/tskit-dev/pyslim/issues>`_ or
`pull request <http://github.com/tskit-dev/pyslim/pulls>`_ on GitHub!
please open an [issue](http://github.com/tskit-dev/pyslim/issues>) or
[pull request](http://github.com/tskit-dev/pyslim/pulls>) on GitHub!

## Quickstart

- Make a fork of the pyslim repo on `GitHub <http://github.com/tskit-dev/pyslim>`_
- Make your own fork of the pyslim repo on [GitHub](http://github.com/tskit-dev/pyslim>)
- Clone your fork into a local directory:

```bash
Expand All @@ -55,3 +55,6 @@ git clone git@github.com:YOUR_GITHUB/pyslim.git
are ready. Please make sure that (a) the tests pass before you open the pull request; and
(b) your code passes PEP8 checks before opening the pull request.

For a more detailed walkthrough of development methods,
see [the stdpopsim documentation](https://popsim-consortium.github.io/stdpopsim-docs/latest/development.html#github-workflow)
and/or [the tskit documentation](https://tskit.dev/tskit/docs/latest/development.html#workflow).
16 changes: 8 additions & 8 deletions docs/tutorial.md
Expand Up @@ -588,13 +588,7 @@ To do this, we need to extract the node IDs from the individuals of the two popu
that are alive at the end of the simulation.

```{code-cell}
pop_indivs = [[], [], []]
pop_nodes = [[], [], []]
for i in ts.individuals_alive_at(0):
ind = ts.individual(i)
pop_indivs[ind.population].append(i)
pop_nodes[ind.population].extend(ind.nodes)
pop_nodes = [ts.samples(population=p, time=0) for p in range(ts.num_populations)]
diversity = ts.diversity(pop_nodes[1:])
divergence = ts.divergence(pop_nodes[1:])
Expand Down Expand Up @@ -687,7 +681,8 @@ and {attr}`.SlimTreeSequence.individual_populations` as follows:
alive = ts.individuals_alive_at(0)
adults = alive[ts.individual_ages[alive] > 2]
pops = [
np.where(ts.individual_populations[adults] == k)[0] for k in [1, 2]
[i for i in adults if ts.individual(i).metadata['subpopulation'] == k]
for k in [1, 2]
]
sample_inds = [np.random.choice(pop, 10, replace=False) for pop in pops]
sample_nodes = []
Expand All @@ -697,6 +692,11 @@ for samp in sample_inds:
sub_ts = ts.simplify(sample_nodes)
```

Note that here we have used the *subpopulation* attribute that SLiM places in metadata
to find out where each individual lives at the end of the simulation.
We might alternatively have used the *population* attribute of Nodes -
but, this would give each individual's *birth* location.

The resulting tree sequence does indeed have fewer individuals and fewer trees:

```{code-cell}
Expand Down
6 changes: 1 addition & 5 deletions docs/vignette_coalescent_diversity.md
Expand Up @@ -315,11 +315,7 @@ times = list(set(ts.individual_times))
times.sort()
print("The times ago at which individuals in the tree sequence were born:", times)
# The times ago at which individuals in the tree sequence were born: [0.0, 100.0]
inds_by_time = [ts.individuals_alive_at(t) for t in times]
nodes_by_time = []
for inds in inds_by_time:
nodes = np.array([ts.individual(i).nodes for i in inds]).flatten()
nodes_by_time.append(nodes)
nodes_by_time = [ts.samples(time=t) for t in times]
num_nodes = np.array([len(x) for x in nodes_by_time])
p = ts.sample_count_stat(nodes_by_time, lambda x: x/num_nodes, 2, windows='sites',
Expand Down
7 changes: 1 addition & 6 deletions docs/vignette_continuing.md
Expand Up @@ -163,12 +163,7 @@ new_nodes = np.where(new_tables.nodes.time == new_time)[0]
print(f"There are {len(new_nodes)} nodes from the start of the new simulation.")
# There are 4425 nodes from the start of the new simulation.
slim_indivs = rts.individuals_alive_at(0)
slim_nodes = []
for ind in slim_indivs:
slim_nodes.extend(ts.individual(ind).nodes)
slim_nodes = np.array(slim_nodes)
slim_nodes = rts.samples(time=0)
assert(len(slim_nodes) == 20000)
# randomly give new_nodes IDs in rts
Expand Down
3 changes: 3 additions & 0 deletions pyslim/slim_tree_sequence.py
Expand Up @@ -170,6 +170,9 @@ class SlimTreeSequence(tskit.TreeSequence):
in the tree sequence: either the last time they were Remembered, or at the end
of the simulation, if they are still alive then.
However, `.individual_populations` gives individuals' birth populations;
for their final location use the subpopulation attribute of metadata.
You can create a :class:`.SlimTreeSequence` using one of
- :meth:`.SlimTreeSequence.load_tables`, :meth:`.SlimTreeSequence.load`,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_tree_sequence.py
Expand Up @@ -543,7 +543,8 @@ def test_post_recap(self, recipe):
@pytest.mark.parametrize('recipe', recipe_eq("everyone"), indirect=True)
def test_post_simplify(self, recipe):
ts = recipe["ts"]
keep_indivs = np.random.choice(
rng = np.random.default_rng(seed=3)
keep_indivs = rng.choice(
np.where(ts.individual_times < ts.metadata['SLiM']['generation'] - 1)[0],
size=30, replace=False)
keep_nodes = []
Expand Down

0 comments on commit 96803a5

Please sign in to comment.