Skip to content

Commit

Permalink
Remove deprecated types from manual page. (#848)
Browse files Browse the repository at this point in the history
* Remove use of WrappedTreeSequence from manual page.
  • Loading branch information
molpopgen committed Nov 4, 2021
1 parent a64a999 commit 4fc3921
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 69 deletions.
93 changes: 24 additions & 69 deletions doc/long_vignettes/tskitconvert_vignette.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This is intentional, as it keeps the execution times to a minimum while still il
---
tags: [ "hide-input" ]
---
import demes
import fwdpy11
```

Expand Down Expand Up @@ -63,52 +64,6 @@ ts.dump("treefile.trees")
We can also do a lot of analysis using this type.
However, certain tasks would be easier if this type knew about the metadata generated by `fwdpy11`.

### A lightweight wrapper

The class {class}`fwdpy11.tskit_tools.WrappedTreeSequence` is a light wrapper around {class}`tskit.TreeSequence`.
This class has properties to easily fetch the top-level metadata.
We use these properties in the examples below.

Instances of this wrapper class can be generated in several ways:

* Directly:

```{code-cell} python
wrapper = fwdpy11.tskit_tools.WrappedTreeSequence(ts=ts)
print(type(wrapper), type(wrapper.ts))
```

* Via a call to {func}`fwdpy11.tskit_tools.load`

* By passing an option when exporting from `fwdpy11`:

```{code-cell} python
ts = pop.dump_tables_to_tskit(wrapped=True)
print(type(ts), type(wrapper.ts))
```

To dump this wrapped type to a file:

```python
# not executed
ts.ts.dump("treefile.trees")
```

In fact, all "`tskit` things" can be done by accessing {attr}`fwdpy11.tskit_tools.WrappedTreeSequence.ts`.

Trying to create the wrapped type from a tree sequence not generated by `fwdpy11` is an error:

```{code-cell} python
---
tags: [raises-exception]
---
import tskit
tc = tskit.TableCollection(100.)
ts = tc.tree_sequence()
wrapper = fwdpy11.tskit_tools.WrappedTreeSequence(ts)
```


## Metadata

The `tskit` data model includes [metadata](https://tskit.dev/tskit/docs/stable/metadata.html).
Expand Down Expand Up @@ -153,20 +108,20 @@ For example, if you built the `rng` object and then simulated two replicates, th
You do not know the state of the `rng` when the second simulation started.

```{code-cell} python
ts = pop.dump_tables_to_tskit(wrapped=True)
assert ts.seed is None
ts = pop.dump_tables_to_tskit()
assert "seed" not in ts.metadata
```

```{code-cell} python
ts = pop.dump_tables_to_tskit(seed=54321, wrapped=True)
assert ts.seed == 54321
ts = pop.dump_tables_to_tskit(seed=54321)
assert ts.metadata["seed"] == 54321
```

```{code-cell} python
---
tags: [raises-exception]
---
ts = pop.dump_tables_to_tskit(seed=-4616, wrapped=True)
ts = pop.dump_tables_to_tskit(seed=-4616)
```

### Adding `ModelParams` objects
Expand All @@ -176,8 +131,8 @@ Including this is optional for several reasons, but a big one is that this class
The examples listed below will work with types provided by `fwdpy11` (although we haven't tested them all!), but user-defined types may be more challenging.

```{code-cell} python
ts = pop.dump_tables_to_tskit(wrapped=True)
assert ts.model_params is None
ts = pop.dump_tables_to_tskit()
assert "model_params" not in ts.metadata
```

Here is an example of Gaussian stabilizing selection around an optimum trait value of `0.0`:
Expand All @@ -199,8 +154,8 @@ params = fwdpy11.ModelParams(**pdict)
We can pass the `params` object on when exporting the data to `tskit`:

```{code-cell} python
ts = pop.dump_tables_to_tskit(model_params=params, wrapped=True)
assert ts.model_params == params
ts = pop.dump_tables_to_tskit(model_params=params)
assert fwdpy11.ModelParams(**eval(ts.metadata["model_params"])) == params
```

Some simulation designs make use of multiple `ModelParams` objects.
Expand Down Expand Up @@ -228,13 +183,13 @@ params2 = fwdpy11.ModelParams(**pdict2)
We can pass both objects to `tskit` via a {class}`dict`:

```{code-cell} python
ts = pop.dump_tables_to_tskit(model_params={'burnin': params, 'adaptation': params2}, wrapped=True)
type(ts.model_params)
ts = pop.dump_tables_to_tskit(model_params={'burnin': params, 'adaptation': params2})
type(ts.metadata["model_params"])
```

```{code-cell} python
assert ts.model_params['burnin'] == params
assert ts.model_params['adaptation'] == params2
assert fwdpy11.ModelParams(**eval(ts.metadata["model_params"]["burnin"])) == params
assert fwdpy11.ModelParams(**eval(ts.metadata["model_params"]["adaptation"])) == params2
```

### Including a `demes` graph
Expand Down Expand Up @@ -301,18 +256,18 @@ migrations:

```{code-cell} python
graph = gutenkunst()
ts = pop.dump_tables_to_tskit(demes_graph=graph, wrapped=True)
ts = pop.dump_tables_to_tskit(demes_graph=graph)
```

```{code-cell} python
assert ts.demes_graph == graph
assert demes.Graph.fromdict(ts.metadata["demes_graph"]) == graph
```

Since this is an optional metadata field, accessing it will return `None` if no graph was provided:

```{code-cell} python
ts = pop.dump_tables_to_tskit(wrapped=True)
assert ts.demes_graph is None
ts = pop.dump_tables_to_tskit()
assert "demes_graph" not in ts.metadata
```

### User-defined metadata
Expand All @@ -323,8 +278,8 @@ For example, this could be a {class}`dict` containing information obtained from
The simplest situation is to use a `dict` containing simple objects:

```{code-cell} python
ts = pop.dump_tables_to_tskit(data={'x': 3}, wrapped=True)
ts.data
ts = pop.dump_tables_to_tskit(data={'x': 3})
ts.metadata["data"]
```

Any use of user-defined types will require conversion to string, however:
Expand All @@ -338,13 +293,13 @@ class MyData(object):
return f"MyData(x={self.x})"
# Send in the str representation:
ts = pop.dump_tables_to_tskit(data=str(MyData(x=77)), wrapped=True)
ts = pop.dump_tables_to_tskit(data=str(MyData(x=77)))
```

Getting the data back out requires an `eval`:

```{code-cell} python
assert eval(ts.data).x == 77
assert eval(ts.metadata["data"]).x == 77
```

:::{warning}
Expand Down Expand Up @@ -384,8 +339,8 @@ To actually illustrate the use of this metadata, we need to make sure that our `
```{code-cell} python
# initialize a population with the right number of demes...
multideme_pop = fwdpy11.DiploidPopulation([100]*len(pop_md), 1.)
ts = multideme_pop.dump_tables_to_tskit(population_metadata=pop_md, wrapped=True)
for pop in ts.ts.populations():
ts = multideme_pop.dump_tables_to_tskit(population_metadata=pop_md)
for pop in ts.populations():
print(pop.metadata)
```

Expand Down
1 change: 1 addition & 0 deletions doc/misc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Dependencies

* Deprecate `fwdpy11.tskit_tools.WrappedTreeSequence`
PR {pr}`841`
PR {pr}`848`

Back end changes:

Expand Down

0 comments on commit 4fc3921

Please sign in to comment.