code: def select_groups(adata, groups_order_subset='all', key='groups'):
      
      groups_order = adata.obs[key].cat.categories
      if key + '_masks' in adata.uns:
          groups_masks = adata.uns[key + '_masks']
      else:
          groups_masks = np.zeros((len(adata.obs[key].cat.categories),
                                   adata.obs[key].values.size), dtype=bool)
          for iname, name in enumerate(adata.obs[key].cat.categories):
              # if the name is not found, fallback to index retrieval
              if adata.obs[key].cat.categories[iname] in adata.obs[key].values:
                  mask = adata.obs[key].cat.categories[iname] == adata.obs[key].values
              else:
                  mask = str(iname) == adata.obs[key].values
              groups_masks[iname] = mask
      groups_ids = list(range(len(groups_order)))
      if groups_order_subset != 'all':
          groups_ids = []
          for name in groups_order_subset:
              groups_ids.append(
                  np.where(adata.obs[key].cat.categories.values == name)[0][0])
          if len(groups_ids) == 0:
              # fallback to index retrieval
              groups_ids = np.where(
                  np.in1d(np.arange(len(adata.obs[key].cat.categories)).astype(str),
                                            np.array(groups_order_subset)))[0]
          if len(groups_ids) == 0:
              logg.m(np.array(groups_order_subset),
                     'invalid! specify valid groups_order (or indices) one of',
                     adata.obs[key].cat.categories)
              from sys import exit
              exit(0)
          groups_masks = groups_masks[groups_ids]
          groups_order_subset = adata.obs[key].cat.categories[groups_ids].values
      else:
          groups_order_subset = groups_order.values
      return groups_order_subset, groups_masks
docs: Get subset of groups in adata.obs[key].