示例#1
0
def purge_unused_annotations(remote_instance=None, force=False, verbose=True):
    if remote_instance in [None, 'source']:
        remote_instance = source_project
        if verbose:
            print('Purging unused annotations from the source project.')
    elif remote_instance == 'target':
        remote_instance = target_project
        if verbose:
            print('Purging unused annotations from the target project.')
    pid = remote_instance.project_id

    tmp_neuron_skids = {2: 6401, 13: 120221, 38: 352154, 59: 665257}
    if pid not in tmp_neuron_skids:
        raise ValueError(
            f'Need to specify a dummy neuron to use for project id {pid}')
    tmp_skid = tmp_neuron_skids[pid]

    all_annots = pymaid.get_annotation_list(remote_instance=remote_instance)

    def add_escapes(s, chars_to_escape='()[]?+'):
        for char in chars_to_escape:
            s = s.replace(char, '\\' + char)
        return s

    def remove_escapes(s):
        while '\\' in s:
            s = s.replace('\\', '')
        return s

    for name in tqdm(all_annots.name):
        name = add_escapes(name)
        try:
            count = len(
                pymaid.get_annotated(name, remote_instance=remote_instance))
            if count > 0:
                pass  #print('{:04d}'.format(count), name)
            else:
                print(name)
                if not force and input('Purge me? [Y/n] ').lower() != 'y':
                    continue
                pymaid.add_annotations(tmp_skid,
                                       remove_escapes(name),
                                       remote_instance=remote_instance)
                pymaid.remove_annotations(tmp_skid,
                                          remove_escapes(name),
                                          remote_instance=remote_instance)
        except:
            print(name)
            raise
示例#2
0
文件: old.py 项目: malei-pku/navis
 def test_annotation_list(self):
     self.assertIsInstance(pymaid.get_annotation_list(),
                           pd.DataFrame)
示例#3
0
def push_all_updates_by_skid(skids, recurse=False, fake=True, **kwargs):
    """
    For each neuron in the source project with one of the given skids,
    search in the target project for neurons that are linked to it, and
    update the target neuron(s) using the appropriate
    manipulate_and_reupload_catmaid_neuron function as specified by the
    linking relation in the "LINKED NEURON" annotation.

    If recurse=True and this function succeeds in performing an update, it
    will then push_all_updates on that updated neuron to try to
    propagate changes through any chains of linked neurons. This
    recursion only happens within the target project. If you need to
    push the updated neuron to a different project, do that manually.
    """
    kwargs['fake'] = fake
    kwargs['refuse_to_update'] = False  # Since this function only does
                                        # updates, refusing to update is
                                        # redundant with 'fake'
    link_types = {
        'copy of': lambda skids: copy_neurons_by_skid(skids, **kwargs),
        'translation of': lambda skids: translate_neurons_by_skid(skids, **kwargs),
        'elastic transformation of':
            lambda skids: elastictransform_neurons_by_skid(skids, **kwargs),
        'elastic transformation and flipped of':
            lambda skids: elastictransform_neurons_by_skid(skids, left_right_flip=True, **kwargs),
        'pruned \(first entry, last exit\) by vol 109 of':  # Note that the \ MUST be included
            lambda skids: volume_prune_neurons_by_skid(skids, 109, **kwargs),
        'radius pruned of':
            lambda skids: radius_prune_neurons_by_skid(skids, **kwargs)
    }

    try:
        iter(skids)
    except:
        skids = [skids]

    if 'skip_dates' in kwargs:
        skip_dates = kwargs.pop('skip_dates')
    else:
        skip_dates = []

    all_target_annots = pymaid.get_annotation_list(remote_instance=target_project)

    original_source_project_id = source_project.project_id
    server_responses = []
    new_skids = skids
    while len(new_skids) > 0:
        new_skids = []
        for source_skid in skids:  # For each skeleton that needs to be pushed
            target_annots = [add_escapes(annot) for annot in all_target_annots.name
                             if 'skeleton id '+str(source_skid)+' ' in annot
                             and 'project id '+str(source_project.project_id)+' 'in annot]
            #print(target_annots)
            # For each annotation that indicates a link to the source skid
            for target_annot in target_annots:
                target_skids = get_skids_by_annotation(target_annot, remote_instance='target')
                if len(target_skids) == 0:
                    continue
                elif len(target_skids) != 1:
                    input('WARNING: Multiple neurons in the target project'
                          ' with the same linking annotation??? Skipping this'
                          f' push: {target_annot}')
                    continue
                if len(skip_dates) > 0:
                    this_target_skid_annots = pymaid.get_annotations(
                            target_skids, remote_instance=target_project)
                # Check what type of link is indicated by this linking annotation
                for linking_relation in link_types:
                    if linking_relation in target_annot:
                        resp = [f'Skipped: {target_annot}']
                        print('Found in project id '
                              f"{target_project.project_id}: '{target_annot}'")
                        if (len(skip_dates) == 0 or not any([any([date in annot for date in skip_dates]) for
                            annot in list(this_target_skid_annots.values())[0]])):
                                resp = link_types[linking_relation](source_skid)
                        else:
                            print(f'Skipping upload because was already updated recently')
                        if recurse and not fake:
                            #new_skids.append(resp[0]['skeleton_id']) # old
                            new_skids.append(target_skids[0])
                        server_responses.extend(resp)
        if recurse and not fake:
            source_project.project_id = target_project.project_id
            skids = new_skids
            print(f'Recursing - now pushing updates to skids {new_skids}')
    if recurse and not fake:
        source_project.project_id = original_source_project_id

    return server_responses