fetch¶
lab.connectome.fetch
¶
Every network fetch in the project.
Writes the config data files that lab.connectome.load reads.
FETCHERS maps each dataset of lab.connectome.load.DATASETS to the
function that writes it.
The fetches need a saved CAVE token, and the census needs
CODEX_API_TOKEN if it is missing.
- cell types and other tables from Codex
- the connections of every proofread neuron, from CAVE
- skeletons, one SWC file per cell, from Codex
FETCHERS = {'census': lambda: download_codex(Sources.CODEX_CENSUS, config.CENSUS), 'visual_types': lambda: download_codex('visual_neuron_types'), 'columns': lambda: download_codex('column_assignment'), 'classification': lambda: download_codex('classification'), 'connections': fetch_connections, 'skeletons': fetch_skeletons}
module-attribute
¶
The function that writes each dataset, by the name in
lab.connectome.load.DATASETS, in the order to run them.
Inventory
¶
Bases: NamedTuple
What the server reports about its materializations.
Examples:
Source code in lab/connectome/fetch.py
Sources
¶
Remote services and the ids of the products fetched from them.
Every id, URL, and limit below is a choice; the comment on each line says why.
Examples:
Source code in lab/connectome/fetch.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
TimeoutAdapter
¶
Bases: HTTPAdapter
Gives every request a timeout.
requests has none by default, and a connection that dies mid-query (a laptop suspended during a long fetch) then waits forever.
Examples:
>>> from unittest import mock
>>> with mock.patch.object(HTTPAdapter, "send") as send:
... _ = TimeoutAdapter().send("request")
... send.call_args.kwargs
{'timeout': 300}
Source code in lab/connectome/fetch.py
send(request, *args, **kwargs)
¶
Send a request, with a default timeout.
batch_connections(outputs, inputs, proofread)
¶
Count the pair weights of one batch of cells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outputs
|
DataFrame
|
Synapses whose presynaptic cell is in the batch. |
required |
inputs
|
DataFrame
|
Synapses whose postsynaptic cell is in the batch. Those from a proofread cell are dropped, since that cell's own batch counts them. |
required |
proofread
|
Series
|
Root ids of every proofread neuron. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Columns |
Examples:
>>> import polars as pl
>>> outputs = pl.DataFrame(
... {"pre_pt_root_id": [2, 2, 2], "post_pt_root_id": [1, 9, 2]}
... )
>>> inputs = pl.DataFrame(
... {"pre_pt_root_id": [1, 9], "post_pt_root_id": [2, 2]}
... )
>>> batch_connections(outputs, inputs, pl.Series([1, 2])).rows()
[(2, 1, 1), (2, 9, 1), (9, 2, 1)]
Source code in lab/connectome/fetch.py
cave_materialize()
¶
Create the CAVE materialize client for Sources.STACK.
Every request carries a timeout, from TimeoutAdapter.
Needs a saved CAVE token.
Returns:
| Type | Description |
|---|---|
MaterializationClient
|
The client. |
See Also
query_synapses : Queries synapse rows with this client. check_inventory : Reads its version and table names.
Examples:
>>> m = cave_materialize()
>>> m.version
783
>>> cell = 720575940606173630
>>> column = "pre_pt_root_id"
>>> rows = query_synapses(m, column, [cell])
>>> rows.columns.tolist()
['id', 'pre_pt_root_id', 'post_pt_root_id']
Source code in lab/connectome/fetch.py
check_inventory(m)
¶
Read the materialization version in use and the table names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
MaterializationClient
|
As |
required |
Returns:
| Type | Description |
|---|---|
Inventory
|
The version in use and the table names. |
Examples:
>>> class Stub:
... version = 783
... def get_tables(self):
... return ["nuclei_v1"]
>>> check_inventory(Stub())
Inventory(version=783, tables=['nuclei_v1'])
Source code in lab/connectome/fetch.py
codex_url(product, api_token)
¶
Build the Codex download URL for one product.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
product
|
str
|
One of |
required |
api_token
|
str
|
The Codex account token. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The URL, in |
Examples:
>>> codex_url("classification", "<token>")
'https://codex.flywire.ai/api/download_resource?data_product=classification&dataset=fafb&api_token=<token>'
Source code in lab/connectome/fetch.py
download_codex(product, dest=None)
¶
Write one Codex product to a file.
Needs Sources.CODEX_ENV in the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
product
|
str
|
One of |
required |
dest
|
Path
|
Where to write; by default |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
The file written. |
Raises:
| Type | Description |
|---|---|
SystemExit
|
When |
See Also
lab.connectome.load.load_census : Reads the census this writes. lab.connectome.load.load_proofread_ids : Reads the classification.
Examples:
The return value is the path of the gzipped CSV:
Its first lines:
root_id,flow,super_class,class,sub_class,hemilineage,side,nerve 720575940599457990,intrinsic,optic,optic_lobe_intrinsic,t4_neuron,,right,
Source code in lab/connectome/fetch.py
fetch_connection_shard(ids, proofread, dest, *, materialize=None)
¶
Fetch the connections of some cells and write their pair weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids
|
list of int
|
The cells of this batch. |
required |
proofread
|
Series
|
Root ids of every proofread neuron. |
required |
dest
|
Path
|
Parquet file to write; it is written under a temporary name first, so a fetch that dies halfway leaves no truncated file. |
required |
materialize
|
MaterializationClient
|
optional
The client; by default this thread's, from |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The number of pairs written. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
From |
Examples:
Cell 10 sends to 11 and 20; both ends are in the answer for the pre column, and 11 is proofread, so its own batch would count the synapses it receives.
>>> import tempfile
>>> class View:
... def query_view(self, view, **kwargs):
... return pd.DataFrame(
... {
... "id": [1, 2],
... "pre_pt_root_id": [10, 10],
... "post_pt_root_id": [20, 11],
... }
... )
>>> dest = Path(tempfile.mkdtemp()) / "part.parquet"
>>> fetch_connection_shard(
... [10], pl.Series([10, 11]), dest, materialize=View()
... )
2
>>> pl.read_parquet(dest).rows()
[(10, 11, 1), (10, 20, 1)]
Source code in lab/connectome/fetch.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 | |
fetch_connections(workers=4)
¶
Fetch every proofread cell's connections and merge them.
The connections go in batches of Sources.BATCH cells to
config.CONNECTIONS_PARTS, and a rerun skips the batches that
are there; the merge writes config.CONNECTIONS and deletes the
folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workers
|
int
|
Batches fetched at once. |
4
|
Raises:
| Type | Description |
|---|---|
SystemExit
|
When any batch failed; rerun to fetch the missing ones. |
See Also
lab.connectome.load.scan_connections : Reads the file this writes. lab.connectome.load.load_connections_of : Reads the rows of some cells. fetch_connection_shard : Fetches one batch.
Examples:
The command asks first and then runs this. A full fetch takes hours and resumes if it is interrupted:
>>> fetch_connections()
0 of 2786 batches on disk; fetching 2786 with 4 workers
...
wrote .../connections.parquet: 99,687,167 pairs
The file has one row per pair of cells:
>>> from lab.connectome.load import scan_connections
>>> scan_connections().head(3).collect()
shape: (3, 3)
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโ
โ pre_pt_root_id โ post_pt_root_id โ weight โ
โ --- โ --- โ --- โ
โ i64 โ i64 โ i64 โ
โโโโโโโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโโโโโชโโโโโโโโโก
โ 720575940379280990 โ 720575940615583647 โ 1 โ
โ 720575940379283592 โ 720575940658814849 โ 1 โ
โ 720575940379284367 โ 720575940614275070 โ 1 โ
โโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโ
Source code in lab/connectome/fetch.py
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 | |
fetch_skeletons(types=SKELETON_TYPES)
¶
Write the first skeletons of each type to config.SKELETONS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
types
|
sequence of str
|
The |
`SKELETON_TYPES`
|
Returns:
| Type | Description |
|---|---|
list of pathlib.Path
|
The |
See Also
skeleton_url : The download URL of one skeleton.
Examples:
Six LC16 skeletons, each named by its root id:
>>> [p.name for p in fetch_skeletons(["LC16"])]
wrote 6 skeletons to .../skeletons
['720575940606173630.swc', '720575940609602770.swc', ...]
Source code in lab/connectome/fetch.py
ids_in_nuclei(m, root_ids)
¶
Count the root ids that nuclei_v1 has a row for.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
MaterializationClient
|
As |
required |
root_ids
|
sequence of int
|
The ids to look up. |
required |
Returns:
| Type | Description |
|---|---|
int
|
How many of them are in the table. |
Examples:
>>> import pandas as pd
>>> class Stub:
... def query_table(self, table, **kwargs):
... return pd.DataFrame({"pt_root_id": [1, 1, 2]})
>>> ids_in_nuclei(Stub(), [1, 2, 3])
2
Source code in lab/connectome/fetch.py
query_synapses(m, column, ids)
¶
Query the synapse rows with a column in some root ids.
A query that hits Sources.ROW_CAP is truncated without an error,
so it is split in half until each answer is under the cap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
MaterializationClient
|
As |
required |
column
|
(pre_pt_root_id, post_pt_root_id)
|
The column to test. |
"pre_pt_root_id"
|
ids
|
list of int
|
Root ids to look for in |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Columns |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
When one root id alone has at least |
Examples:
>>> class View:
... def query_view(self, view, **kwargs):
... return pd.DataFrame(
... {
... "id": [1, 2],
... "pre_pt_root_id": [5, 6],
... "post_pt_root_id": [7, 8],
... "extra": [0, 0],
... }
... )
>>> rows = query_synapses(View(), "pre_pt_root_id", [5, 6])
>>> rows.columns.tolist()
['id', 'pre_pt_root_id', 'post_pt_root_id']
Source code in lab/connectome/fetch.py
sample_tables(m)
¶
Query a few rows of the tables every fetch reads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
MaterializationClient
|
As |
required |
Returns:
| Type | Description |
|---|---|
nuclei, view_row, synapse : pandas.DataFrame
|
Three rows of |
Examples:
>>> import pandas as pd
>>> class Stub:
... def query_table(self, table, **kwargs):
... return pd.DataFrame({"table": [table]})
... def query_view(self, view, **kwargs):
... return pd.DataFrame({"table": [view]})
>>> nuclei, view_row, synapse = sample_tables(Stub())
>>> nuclei["table"].tolist()
['nuclei_v1']
Source code in lab/connectome/fetch.py
skeleton_url(root_id)
¶
Build the Codex SWC URL for one root id.
This is the only skeleton source besides the bulk zip on their download page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_id
|
int
|
The cell. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The URL under |
Examples: