-
Notifications
You must be signed in to change notification settings - Fork 84
metadata access counter; closes #3472 #3475
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
Open
petrelharp
wants to merge
5
commits into
tskit-dev:main
Choose a base branch
from
petrelharp:metadata_cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -153,6 +153,69 @@ time *units* of a tree sequence should be stored in the | |||||
| metadata. For example, we could set `tables.time_units = "generations"`. | ||||||
| ::: | ||||||
|
|
||||||
|
|
||||||
| (sec_metadata_top_level_pitfalls)= | ||||||
|
|
||||||
| ### Pitfalls related to metadata | ||||||
|
|
||||||
| Briefly, the pitfalls are: | ||||||
|
|
||||||
| 1. editing metadata directly can silently do nothing, and | ||||||
| 2. making repeated calls to top-level metadata will make your code very slow | ||||||
| if that metadata is large. | ||||||
|
|
||||||
| One thing that it is important to know about metadata is that metadata access methods | ||||||
| return a *copy* of the *decoded* information. | ||||||
| So, for instance, each time you call | ||||||
| ```{code-cell} | ||||||
| tables.metadata | ||||||
| ``` | ||||||
| (or ``ts.metadata``), it gives you a dictionary containing a *new copy* of the | ||||||
| underlying information. So, this means that you cannot edit metadata | ||||||
| by direct assignment. For instance, trying to change the metadata like this | ||||||
| silently does nothing: | ||||||
| ```{code-cell} | ||||||
| tables.metadata["taxonomy"]["subspecies"] = "lyrata" | ||||||
| print(tables.metadata["taxonomy"]["subspecies"]) | ||||||
| ``` | ||||||
| This is true for all types of metadata: | ||||||
| for instance, editing `ts.mutation(0).metadata` will not change the metadata | ||||||
| of the underlying mutation. | ||||||
| To edit top-level metadata, first copy out the metadata, edit *that*, | ||||||
| and then put it back: | ||||||
| ```{code-cell} | ||||||
| md = tables.metadata | ||||||
| md["taxonomy"]["species"] = "lyrata" | ||||||
| tables.metadata = md | ||||||
| ``` | ||||||
|
|
||||||
| This fact about metadata can have important consequences for performance. | ||||||
| For instance, if we'd like to convert all mutation times to generations, | ||||||
| we should **definitely not** run | ||||||
| ``[mut.time / ts.metadata["generation_time"] for mut in ts.mutations()]``. | ||||||
| This is because a new copy of the top-level metadata will be created | ||||||
| for *every* mutation in the tree sequence. | ||||||
| This may not be a big deal for some tree sequences, | ||||||
| but this will take prohibitively long for tree sequences | ||||||
| that have a large amount of information in top-level metadata | ||||||
| (for instance, those produced by SLiM v6). | ||||||
| Instead, it is good practice to make a copy of the top-level metadata, | ||||||
| and use that copy henceforth, like so: | ||||||
| ```{code-cell} | ||||||
| :tags: ["skip-execution"] | ||||||
| top_md = ts.metadata | ||||||
| mut_times = [ | ||||||
| mut.time / top_md["generation_time"] for mut in ts.mutations() | ||||||
| ] | ||||||
| ``` | ||||||
|
|
||||||
| Because of this, tskit will produce a Warning if the top-level metadata | ||||||
| is large (greater than 100Kb) and is accessed many times (more than 20 times). | ||||||
| This behavior can be changed, using | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| {data}`tskit.METADATA_ACCESS_WARNING_THRESHHOLD` | ||||||
| and {data}`tskit.METADATA_ACCESS_WARNING_SIZE`. | ||||||
|
|
||||||
|
|
||||||
| (sec_metadata_examples_reference_sequence)= | ||||||
|
|
||||||
| ### Reference sequence | ||||||
|
|
@@ -284,8 +347,9 @@ must be encoded and decoded. The C API does not do this, but the Python API will | |||||
| use the schema to decode the metadata to Python objects. | ||||||
| The encoding for doing this is specified in the top-level schema property `codec`. | ||||||
| Currently the Python API supports the `json` codec which encodes metadata as | ||||||
| [JSON](https://www.json.org/json-en.html), and the `struct` codec which encodes | ||||||
| metadata in an efficient schema-defined binary format using {func}`python:struct.pack` . | ||||||
| [JSON](https://www.json.org/json-en.html), the `struct` codec which encodes | ||||||
| metadata in an efficient schema-defined binary format using {func}`python:struct.pack`, | ||||||
| and the `json+struct` codec which is a combination of the two. | ||||||
|
|
||||||
| (sec_metadata_codecs_json)= | ||||||
|
|
||||||
|
|
@@ -436,6 +500,12 @@ The supported numeric and boolean types are: | |||||
| - 8 | ||||||
| ``` | ||||||
|
|
||||||
| In addition to the `binaryFormat` encoding given in the table above, | ||||||
| the `type` key must also be set to the appropriate value. | ||||||
| For boolean values the `type` should be `boolean` (not bool); | ||||||
| for integer binary formats it should be `integer` (not number), | ||||||
| and for floating-point binary formats it should be `number`. | ||||||
|
|
||||||
| When attempting to pack a non-integer using any of the integer conversion | ||||||
| codes, if the non-integer has a `__index__` method then that method is | ||||||
| called to convert the argument to an integer before packing. | ||||||
|
|
@@ -602,7 +672,7 @@ into 8-byte alignment; and | |||||
| (7) the binary data. | ||||||
| The JSON data is encoded as ASCII, without a null terminating byte, | ||||||
| and the format of the binary data is specified using the "struct" portion | ||||||
| of the metadata schema, described :ref:`above <sec_metadata_codecs_struct>`. | ||||||
| of the metadata schema, described {ref}`above <sec_metadata_codecs_struct>`. | ||||||
|
|
||||||
| (sec_metadata_schema_examples)= | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the following sentence deals with top-level metadata specifically, this seems like a good place to point to instructions on how to replace the table-specific metadata (e.g. "Replacing metadata within a table (such as a
MutationTable) is described in XXXX. To edit top level metadata ...").