Esempio n. 1
0
    def default_celltypes(exclude = []):
        priority_list = pymaid.get_annotated('mw brain simple priorities').name
        priority_skids = [Celltype_Analyzer.get_skids_from_meta_meta_annotation(priority) for priority in priority_list]

        # made the priority groups exclusive by removing neurons from lists that also in higher priority
        override = priority_skids[0]
        priority_skids_unique = [priority_skids[0]]
        for i in range(1, len(priority_skids)):
            skids_temp = list(np.setdiff1d(priority_skids[i], override))
            priority_skids_unique.append(skids_temp)
            override = override + skids_temp

        # take all 'mw brain simple groups' skids (under 'mw brain simple priorities' meta-annotation)
        #   and remove skids that aren't in the appropriate priority_skids_unique level
        priority_skid_groups = [list(pymaid.get_annotated(meta).name) for meta in priority_list]

        skid_groups = []
        for i in range(0, len(priority_skid_groups)):
            group = []
            for j in range(0, len(priority_skid_groups[i])):
                skids_temp = pymaid.get_skids_by_annotation(pymaid.get_annotated(priority_skid_groups[i][j]).name)
                skids_temp = list(np.intersect1d(skids_temp, priority_skids_unique[i])) # make sure skid in subgroup is set in correct priority list
                
                # remove neurons in optional "exclude" list
                if(len(exclude)>0):
                    skids_temp = list(np.setdiff1d(skids_temp, exclude))

                group.append(skids_temp)
            skid_groups.append(group)

        # test skid counts for each group
        #[len(x) for sublist in skid_groups for x in sublist]

        # make list of lists of skids + their associated names
        skid_groups = [x for sublist in skid_groups for x in sublist]
        names = [list(pymaid.get_annotated(x).name) for x in priority_list]
        names = [x for sublist in names for x in sublist]
        names = [x.replace('mw brain ', '') for x in names]
        
        # identify colors
        colors = list(pymaid.get_annotated('mw brain simple colors').name)

        # official order; note that it will have to change if any new groups are added
        official_order = ['sensories', 'PNs', 'ascendings', 'PNs-somato', 'LNs', 'LHNs', 'FFNs', 'MBINs', 'KCs', 'MBONs', 'MB-FBNs', 'CNs', 'pre-dSEZs', 'pre-dVNCs', 'RGNs', 'dSEZs', 'dVNCs']
        colors_names = [x.name.values[0] for x in list(map(pymaid.get_annotated, colors))] # use order of colors annotation for now
        if(len(official_order)!=len(colors_names)):
            print('warning: issue with annotations! Check "official_order" in Celltype_Analyzer.default_celltypes()')
            
        # ordered properly and linked to colors
        groups_sort = [np.where(x==np.array(official_order))[0][0] for x in names]
        names = [element for _, element in sorted(zip(groups_sort, names))]
        skid_groups = [element for _, element in sorted(zip(groups_sort, skid_groups))]

        color_sort = [np.where(x.replace('mw brain ', '')==np.array(official_order))[0][0] for x in colors_names]
        colors = [element for _, element in sorted(zip(color_sort, colors))]

        data = pd.DataFrame(zip(names, skid_groups, colors), columns = ['name', 'skids', 'color'])
        celltype_objs = list(map(lambda x: Celltype(*x), zip(names, skid_groups, colors)))

        return(data, celltype_objs)
Esempio n. 2
0
 def get_skids_from_meta_meta_annotation(meta_meta, split=False, return_celltypes=False):
     meta_annots = pymaid.get_annotated(meta_meta).name
     annot_list = [list(pymaid.get_annotated(meta).name) for meta in meta_annots]
     skids = [list(pymaid.get_skids_by_annotation(annots)) for annots in annot_list]
     if(split==False):
         skids = [x for sublist in skids for x in sublist]
         return(skids)
     if(split==True):
         if(return_celltypes==True):
             celltypes = [Celltype(meta_annots[i], skids[i]) for i in range(len(meta_annots))]
             return(celltypes)
         if(return_celltypes==False):
             return(skids, meta_annots)
Esempio n. 3
0
 def get_skids_from_meta_annotation(meta, split=False, unique=True, return_celltypes=False):
     annot_list = pymaid.get_annotated(meta).name
     skids = [list(pymaid.get_skids_by_annotation(annots)) for annots in annot_list]
     if(split==False):
         skids = [x for sublist in skids for x in sublist]
         if(unique==True):
             skids = list(np.unique(skids))
         return(skids)
     if(split==True):
         if(return_celltypes==True):
             celltypes = [Celltype(annot_list[i], skids[i]) for i in range(len(annot_list))]
             return(celltypes)
         if(return_celltypes==False):
             return(skids, annot_list)
Esempio n. 4
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
Esempio n. 5
0
def df_from_meta_annotation(key, filt=None):
    print(f"Getting annotations under {key}:\n")
    annot_df = pymaid.get_annotated(key)

    series_ids = []

    for annot_name in annot_df["name"]:
        print("\t" + annot_name)
        ids = pymaid.get_skids_by_annotation(annot_name.replace("*", "\*"))
        if filt is not None:
            name = filt(annot_name)
        else:
            name = annot_name
        indicator = pd.Series(index=ids,
                              data=np.ones(len(ids), dtype=bool),
                              name=name,
                              dtype=bool)
        series_ids.append(indicator)
    print()
    return pd.concat(series_ids, axis=1, ignore_index=False)
Esempio n. 6
0
 def test_get_annotated(self):
     self.assertIsInstance(pymaid.get_annotated(
         config_test.test_annotations[0], remote_instance=self.rm),
                                                pd.DataFrame)
Esempio n. 7
0
            sc = get_single_class(classes[inds])
        else:
            if fill_unk:
                sc = "unk"
            else:
                sc = ""
        single_class.append(sc)
    return single_class, all_class, n_class


# %% [markdown]
# ##
print("Loading annotations:\n")

start_instance()
annot_df = pymaid.get_annotated("mw neuron groups")

series_ids = []
for annot_name in annot_df["name"]:
    print(annot_name)
    ids = pymaid.get_skids_by_annotation(annot_name)
    name = annot_name.replace("mw ", "")
    name = name.replace(" ", "_")
    indicator = pd.Series(index=ids,
                          data=np.ones(len(ids), dtype=bool),
                          name=name,
                          dtype=bool)
    series_ids.append(indicator)
    print()

# %% [markdown]