Beispiel #1
0
def get_mesh_neuron(id, with_synapses=False, dataset='production'):
    """Fetch flywire neuron as navis.MeshNeuron.

    Parameters
    ----------
    id  :                int | list of int
                         Segment ID(s) to fetch meshes for.
    with_synapses :      bool
                         If True, will also load a connector table with
                         synapse predicted by Buhmann et al. (2020).
                         A "synapse score" (confidence) threshold of 30 is
                         applied.
    dataset :            str | CloudVolume
                         Against which flywire dataset to query::
                           - "production" (currently fly_v31)
                           - "sandbox" (currently fly_v26)

    Return
    ------
    navis.MeshNeuron

    Examples
    --------
    >>> from fafbseg import flywire
    >>> m = flywire.get_mesh_neuron(720575940614131061)
    >>> m.plot3d()  # doctest: +SKIP

    """
    vol = parse_volume(dataset)

    if navis.utils.is_iterable(id):
        return navis.NeuronList([
            get_mesh_neuron(n, dataset=dataset, with_synapses=with_synapses)
            for n in navis.config.tqdm(id, desc='Fetching', leave=False)
        ])

    # Make sure the ID is integer
    id = int(id)

    # Fetch mesh
    mesh = vol.mesh.get(id, remove_duplicate_vertices=True)[id]

    # Turn into meshneuron
    n = navis.MeshNeuron(mesh, id=id, units='nm', dataset=dataset)

    if with_synapses:
        _ = fetch_synapses(n,
                           attach=True,
                           min_score=30,
                           dataset=dataset,
                           progress=False)

    return n
Beispiel #2
0
def get_mesh_neuron(id, dataset='production'):
    """Fetch flywire neuron as navis.MeshNeuron.

    Parameters
    ----------
    id  :                int | list of int
                         Segment ID(s) to fetch meshes for.
    dataset :            str | CloudVolume
                         Against which flywire dataset to query::
                           - "production" (currently fly_v31)
                           - "sandbox" (currently fly_v26)

    Return
    ------
    navis.MeshNeuron

    Examples
    --------
    >>> from fafbseg import flywire
    >>> m = flywire.get_mesh_neuron(720575940614131061)
    >>> m.plot3d()  # doctest

    """
    vol = parse_volume(dataset)

    if navis.utils.is_iterable(id):
        return navis.NeuronList([
            get_mesh_neuron(n, dataset=dataset)
            for n in tqdm(id, desc='Fetching', leave=False)
        ])

    # Make sure the ID is integer
    id = int(id)

    # Fetch mesh
    mesh = vol.mesh.get(id, remove_duplicate_vertices=True)[id]

    # Turn into meshneuron
    return navis.MeshNeuron(mesh, id=id, units='nm', dataset=dataset)
Beispiel #3
0
def merge_flywire_neuron(id,
                         target_instance,
                         tag,
                         flywire_dataset='production',
                         assert_id_match=True,
                         drop_soma_hairball=True,
                         **kwargs):
    """Merge flywire neuron into FAFB.

    This function (1) fetches a mesh from flywire, (2) turns it into a skeleton,
    (3) maps the coordinates to FAFB 14 and (4) runs ``fafbseg.merge_neuron``
    to merge the skeleton into CATMAID. See Examples below on how to run these
    individual steps yourself if you want more control over e.g. how the mesh
    is skeletonized.

    Parameters
    ----------
    id  :                int
                         ID of the flywire neuron you want to merge.
    target_instance :    pymaid.CatmaidInstance
                         Instance to merge the neuron into into.
    tag :                str
                         You personal tag to add as annotation once import into
                         CATMAID is complete.
    dataset :            str | CloudVolume
                         Against which flywire dataset to query::
                            - "production" (current production dataset, fly_v31)
                            - "sandbox" (i.e. fly_v26)
    assert_id_match :    bool
                         If True, will check if skeleton nodes map to the
                         correct segment ID and if not will move them back into
                         the segment. This is potentially very slow!
    drop_soma_hairball : bool
                         If True, we will try to drop the hairball that is
                         typically created inside the soma.
    **kwargs
                Keyword arguments are passed on to ``fafbseg.merge_neuron``.

    Examples
    --------
    # Import flywire neuron
    >>> _ = merge_flywire_neuron(id=720575940610453042,
    ...                          cvpath='graphene://https://prodv1.flywire-daf.com/segmentation/1.0/fly_v26',
    ...                          target_instance=manual,
    ...                          tag='WTCam')

    """
    if not sk:
        raise ImportError('Must install skeletor: pip3 install skeletor')

    vol = parse_volume(flywire_dataset)

    # Make sure this is a valid integer
    id = int(id)

    # Download the mesh
    mesh = vol.mesh.get(id, deduplicate_chunk_boundaries=False)[id]

    # Convert to neuron
    n_fw, simp, cntr = skeletonize_neuron(
        mesh,
        drop_soma_hairball=drop_soma_hairball,
        dataset=flywire_dataset,
        assert_id_match=assert_id_match)

    # Confirm
    viewer = navis.Viewer(title='Confirm skeletonization')
    # Make sure viewer is actually visible and cleared
    viewer.show()
    viewer.clear()
    # Add skeleton
    viewer.add(n_fw, color='r')

    msg = """
    Please carefully inspect the skeletonization of the flywire mesh.
    Hit ENTER to proceed if happy or CTRL-C to cancel.
    """

    # Add mesh last - otherwise it might mask out other objects despite alpha
    viewer.add(navis.MeshNeuron(mesh), color='w', alpha=.2)

    try:
        _ = input(msg)
    except KeyboardInterrupt:
        raise KeyboardInterrupt('Merge process aborted by user.')
    except BaseException:
        raise
    finally:
        viewer.close()

    # Xform to FAFB
    n_fafb = xform.flywire_to_fafb14(n_fw,
                                     on_fail='raise',
                                     coordinates='nm',
                                     inplace=False)
    mesh_fafb = xform.flywire_to_fafb14(tm.Trimesh(mesh.vertices, mesh.faces),
                                        on_fail='raise',
                                        coordinates='nm',
                                        inplace=False)

    # Heal neuron
    n_fafb = navis.heal_fragmented_neuron(n_fafb)

    # Merge neuron
    return merge_into_catmaid(n_fafb,
                              target_instance=target_instance,
                              tag=tag,
                              mesh=mesh_fafb,
                              **kwargs)
Beispiel #4
0
def merge_flywire_neuron(id,
                         target_instance,
                         tag,
                         flywire_dataset='production',
                         assert_id_match=True,
                         drop_soma_hairball=True,
                         **kwargs):
    """Merge FlyWire neuron into FAFB CATMAID.

    This function (1) fetches a mesh from FlyWire, (2) turns it into a skeleton,
    (3) maps the coordinates to FAFB v14 space and (4) runs
    ``fafbseg.merge_neuron`` to merge the skeleton into CATMAID.

    Disclaimer:

     1. It is your responsibility to make sure that your export of FlyWire data
        does not conflict with the FlyWire community guidelines. Mass export of
        reconstructions is not OK!
     2. As with all imports to CATMAID, the importing user is responsible for
        the quality of the imported skeleton and to make sure no existing
        tracings (including annotations) are negatively impacted.

    Parameters
    ----------
    id  :                int
                         ID of the FlyWire neuron you want to merge.
    target_instance :    pymaid.CatmaidInstance
                         Instance to merge the neuron into into.
    tag :                str
                         You personal tag to add as annotation once import into
                         CATMAID is complete.
    dataset :            str | CloudVolume
                         Against which FlyWire dataset to query::
                            - "production" (current production dataset, fly_v31)
                            - "sandbox" (i.e. fly_v26)
    assert_id_match :    bool
                         If True, will check if skeleton nodes map to the
                         correct segment ID and if not will move them back into
                         the segment. This is potentially very slow!
    drop_soma_hairball : bool
                         If True, we will try to drop the hairball that is
                         typically created inside the soma.
    **kwargs
                Keyword arguments are passed on to ``fafbseg.merge_neuron``.

    Examples
    --------
    Import a FlyWire neuron:

    >>> _ = fafbseg.flywire.merge_flywire_neuron(id=720575940610453042,
    ...                                          target_instance=manual,
    ...                                          tag='WTCam')

    """
    if not sk:
        raise ImportError('Must install skeletor: pip3 install skeletor')

    vol = parse_volume(flywire_dataset)

    # Make sure this is a valid integer
    id = int(id)

    # Download the mesh
    mesh = vol.mesh.get(id, deduplicate_chunk_boundaries=False)[id]

    # Convert to neuron
    n_fw = skeletonize_neuron(mesh,
                              remove_soma_hairball=drop_soma_hairball,
                              dataset=flywire_dataset,
                              assert_id_match=assert_id_match)

    # Confirm
    viewer = navis.Viewer(title='Confirm skeletonization')
    # Make sure viewer is actually visible and cleared
    viewer.show()
    viewer.clear()
    # Add skeleton
    viewer.add(n_fw, color='r')

    msg = """
    Please carefully inspect the skeletonization of the FlyWire neuron.
    Hit ENTER to proceed if happy or CTRL-C to cancel.
    """

    # Add mesh last - otherwise it might mask out other objects despite alpha
    viewer.add(navis.MeshNeuron(mesh), color='w', alpha=.2)

    try:
        _ = input(msg)
    except KeyboardInterrupt:
        raise KeyboardInterrupt('Merge process aborted by user.')
    except BaseException:
        raise
    finally:
        viewer.close()

    # Xform to FAFB
    n_fafb = xform.flywire_to_fafb14(n_fw,
                                     on_fail='raise',
                                     coordinates='nm',
                                     inplace=False)
    mesh_fafb = xform.flywire_to_fafb14(tm.Trimesh(mesh.vertices, mesh.faces),
                                        on_fail='raise',
                                        coordinates='nm',
                                        inplace=False)

    # Heal neuron
    n_fafb = navis.heal_fragmented_neuron(n_fafb)

    # Merge neuron
    return merge_into_catmaid(n_fafb,
                              target_instance=target_instance,
                              tag=tag,
                              mesh=mesh_fafb,
                              **kwargs)